【发布时间】:2016-06-10 09:15:42
【问题描述】:
目前,我的应用由屏幕底部的EditText 和按钮组成。如果我在EditText 中输入内容,然后点击该按钮,则会在屏幕顶部创建TextView。
但是,我想要实现的是,如果我点击那个按钮,TextView 会在 EditText 字段的正上方创建。如果我再次这样做,我希望再次在 EditText 上方创建第二个 TextView,但在另一个 TextView 下方创建。
这是我目前的代码。
这是 xml:(这都在 RelativeLayout 中。但由于某种原因,我不能把它放在这里,因为它没有被检测为代码......)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Senden"
android:layout_gravity="center_horizontal"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="470px"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
还有我的活动:
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
final View.OnClickListener btnHandler = new View.OnClickListener() {
public void onClick(View v) {
String msg = editText.getText().toString();
editText.getText().clear();
linearLayout.addView(createNewTextView(msg));
View view = getCurrentFocus();
if(view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
};
button.setOnClickListener(btnHandler);
}
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);
textView.setLayoutParams(lparams);
textView.setTextColor(Color.BLACK);
textView.setText(text);
return textView;
}
我需要改变什么才能让它看起来像我想要的那样?
【问题讨论】:
标签: android android-layout android-studio