我可以成功地添加一个文本视图按钮点击滚动视图,如两个屏幕截图所示:screenshot 1和screenshot 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;
}
感谢您的回应,但相对布局不能解决问题。 – 4rsenal
可以请你发布你的活动课程代码吗? – FAT
刚刚添加活动代码 – 4rsenal