将文本视图动态添加到表格布局
您好,下面是我的前端和后端代码。我想要做的是将文本视图和行动态添加到 xml 的表布局部分。有人看出有什么问题吗?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relLayout" android:layout_height="wrap_content"
android:layout_width="fill_parent">
<EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:text="Enter Stock to Add" android:textSize="15px"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_marginLeft="20px" android:layout_marginRight="20px" android:layout_marginTop="20px"></EditText>
<Button android:id="@+id/addButton" android:layout_width="100px"
android:layout_below="@id/editText1" android:layout_alignParentRight="true"
android:layout_height="wrap_content" android:text="Add Stock" android:layout_marginRight="20px" android:textSize="15px"></Button>
</RelativeLayout>
<TableLayout android:id="@+id/tableLayout1" android:layout_height="wrap_content" android:layout_width="fill_parent">
</TableLayout>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
//set the layout of the view
setContentView(R.layout.main);
Hi below is my code for the front end and backend. What I'm trying to do is add a textview and a row to the table layout portion of the xml dynamically. Does anyone see anything wrong?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relLayout" android:layout_height="wrap_content"
android:layout_width="fill_parent">
<EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:text="Enter Stock to Add" android:textSize="15px"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_marginLeft="20px" android:layout_marginRight="20px" android:layout_marginTop="20px"></EditText>
<Button android:id="@+id/addButton" android:layout_width="100px"
android:layout_below="@id/editText1" android:layout_alignParentRight="true"
android:layout_height="wrap_content" android:text="Add Stock" android:layout_marginRight="20px" android:textSize="15px"></Button>
</RelativeLayout>
<TableLayout android:id="@+id/tableLayout1" android:layout_height="wrap_content" android:layout_width="fill_parent">
</TableLayout>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
//set the layout of the view
setContentView(R.layout.main);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试调用 setContentView(R.layout.main);在添加新元素之前?
did you try to call setContentView(R.layout.main); before you add the new element?