我对开发应用程序还很陌生。我认为这是一个基本操作,所以如果已经有解决方案的线程,我会接受链接。但是由于我已经搜索了两个小时,仍然没有找到答案,所以我还是要问一下:
我想每次用户点击按钮时动态地向我的布局中添加一个元素。
目前我有以下代码:
XML(R.layout.game.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit_choice"
android:onClick="submitChoice"/>
</LinearLayout>
Java
public void submitChoice(View view)
{
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
LinearLayout ll = new LinearLayout(this);
ll.addView(View.inflate(ll.getContext(), R.layout.game, null));
ll.addView(textView);
setContentView(ll);
}
由于XML文件不会改变,它只能工作一次。
那么当用户第二次点击按钮时如何添加第二个文本(而不更改XML文件)?可以提供示例。