2017-05-07 54 views
0

我可以成功地添加一個文本視圖按鈕點擊滾動視圖,如兩個屏幕截圖所示:screenshot 1screenshot 2。但是如果我想要將textviews首先添加到屏幕底部,並且每個添加的textview將前一個textview推到其上方,那麼這會導致我向上滾動而不是向下查看過去的文字瀏覽?我一直在努力實現這一點,並且無法實現它。有什麼建議麼?我試圖簡單地改變線性佈局佈局重心其工作,直到渦卷部分的用武之地。嘗試添加textview按鈕點擊滾動視圖

<ScrollView 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_marginRight="8dp" 
    app:layout_constraintRight_toRightOf="parent" 
    android:layout_marginLeft="8dp" 
    app:layout_constraintLeft_toLeftOf="parent" 
    android:layout_marginBottom="8dp" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginTop="8dp" 
    app:layout_constraintBottom_toTopOf="@+id/editText"> 
    <LinearLayout 
     android:id="@+id/layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="8dp" 
     android:orientation="vertical" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"> 
    </LinearLayout> 
</ScrollView> 
<EditText 
    android:id="@+id/editText" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="4dp" 
    android:layout_marginEnd="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginStart="8dp" 
    android:ems="10" 
    android:inputType="textPersonName" 
    android:hint="Enter Text" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.508" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toLeftOf="@+id/button" /> 
<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Create" 
    app:layout_constraintBottom_toBottomOf="parent" 
    android:layout_marginBottom="1dp" 
    android:layout_marginRight="8dp" 
    app:layout_constraintRight_toRightOf="parent" 
    android:layout_marginEnd="8dp" /> 


private LinearLayout layout; 
private EditText editText; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    layout = (LinearLayout) findViewById(R.id.layout); 
    editText = (EditText) findViewById(R.id.editText); 
    Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(onClick()); 
} 
private View.OnClickListener onClick() { 
    return new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      layout.addView(createNewTextView(editText.getText().toString()),0); 
     } 
    }; 
} 
private TextView createNewTextView(String text) { 
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams 
      (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    final TextView textView = new TextView(this); 
    lparams.gravity = Gravity.BOTTOM; 
    textView.setLayoutParams(lparams); 
    textView.setText("New text: " + text); 
    textView.setTextSize(30);; 
    return textView; 
} 

回答

0

添加新的TextView後,你可以調用scrollView.fullScroll(ScrollView.FOCUS_DOWN);

在你的XML佈局代碼:

<ScrollView 
    android:id="@+id/scrollview" 
    <!-- this adds an id for your scrollview --> 
    <!-- rest of your XML layout below --> 

在你的活動代碼:

private ScrollView scrollView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // Add the scrollView reference 
    this.scrollView = (ScrollView) findViewById(R.id.scrollview); 
    ...rest of onCreate 

private View.OnClickListener onClick() { 
    return new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      layout.addView(createNewTextView(editText.getText().toString()),0); 
      // Scroll the scrollView to the bottom 
      scrollView.fullScroll(ScrollView.FOCUS_DOWN); 
     } 
    }; 
} 

來源:How to scroll to bottom in a ScrollView on activity startup

提示:而不是增加TextViews成滾動型的,你可以使用ListView或RecyclerView。

1

嘗試使用RelativeLayout作爲根佈局。

試試這個:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/background"> 

    <LinearLayout 
     android:id="@+id/bottom_layout" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_weight="4"> 
     <EditText 
      android:id="@+id/edit" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
     <Button 
      android:id="@+id/butt" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="GO" 
      android:layout_weight="4"/> 
    </LinearLayout> 

    <ScrollView 
     android:id="@+id/scroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/bottom_layout"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:id="@+id/viewLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom"> 


     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 
+0

感謝您的迴應,但相對佈局不能解決問題。 – 4rsenal

+0

可以請你發佈你的活動課程代碼嗎? – FAT

+0

剛剛添加活動代碼 – 4rsenal