我遇到以下代码的问题:
TextView[] tx = new TextView[10];
TableRow[] tr = new TableRow[10];
final TableLayout tl = (TableLayout)findViewById(R.id.maintable);
for(i = 0; i < 10; i++)
{
tx[i] = new TextView(TransactionsActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx[i].setText(Dates1);
tr[i] = new TableRow(TransactionsActivity.this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i]);
tl.addView(tr[i], new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
我要做的是将textview动态添加到表格行或布局。但是当我运行它时,这段代码什么都不做,但是没有错误。
我的XML代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/energy">
<ScrollView
android:id="@+id/scrllvwNo1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Screen Design for VIDEOS -->
<TextView android:id="@+id/textview1"
android:text="Transactions page"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button"
android:text="Clear Transactions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textview2"
android:layout_below="@id/textview1"/>
<TextView android:id="@+id/textview2"
android:text=""
android:padding="15dip"
android:textSize="18dip"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_centerHorizontal="true"/>
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Transactions"
android:layout_toLeftOf="@id/textview2"
android:layout_alignTop="@id/button"/>
<TextView android:id="@+id/textview"
android:text=""
android:padding="0dip"
android:textSize="0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button"/>
<HorizontalScrollView android:id="@+id/horz"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/textview">
<TableLayout android:id="@+id/maintable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="4"
android:layout_below="@id/horz">
<TableRow>
<TextView
android:id="@+id/textview1"
android:layout_column="1"
android:text="Date"
android:padding="15dip" />
<TextView
android:id="@+id/textview2"
android:layout_column="2"
android:text="Device"
android:layout_toLeftOf="@id/textview1"
android:padding="15dip" />
<TextView
android:id="@+id/textview3"
android:layout_column="3"
android:text="Access Type"
android:layout_toLeftOf="@id/textview2"
android:padding="15dip" />
<TextView
android:id="@+id/textview4"
android:layout_column="4"
android:text="Area"
android:layout_toLeftOf="@id/textview3"
android:padding="15dip" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>
</HorizontalScrollView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:0)
问题可能是没有为textviews指定布局参数。试试这个
tx[i].setLayoutParams(new LayoutParams(ViewGroup.WRAP_CONTENT,
ViewGroup.WRAP_CONTENT));
还应将tr数组添加到tablelayout
答案 1 :(得分:0)
我刚刚删除了为TextView添加LayoutParams并使其正常工作。我不知道为什么。
示例代码:
for(int i = 0; i < 10; i++)
{
tx[i] = new TextView(this);
tx[i].setText(Dates1);
tr[i] = new TableRow(this);
tr[i].setLayoutParams(
new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i]);
tl.addView(tr[i], new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}