【问题标题】:Adding textview dynamically to table layout将文本视图动态添加到表格布局
【发布时间】:2011-05-05 15:10:57
【问题描述】:

您好,下面是我的前端和后端代码。我想要做的是动态添加一个文本视图和一行到 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);

【问题讨论】:

  • 你没有真正描述什么是问题,你为什么认为有问题..

标签: android textview tablelayout


【解决方案1】:

您是否尝试调用 setContentView(R.layout.main);在添加新元素之前?

【讨论】:

  • 我猜 setContentView 调用将从 xml 描述中生成布局元素。因此,当您在此之前修改元素时,这些修改将丢失。但这只是我的猜测。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 2014-03-28
相关资源
最近更新 更多