我一直在玩Android的表格布局,并且一直在尝试以编程方式将2个文本视图放在同一个单元格中。我对这个主题进行了一些研究,发现我需要使用布局才能在同一个单元格中添加多个元素。
我使用RelatvieLayout完成了这项工作,但出于某些原因,我的一些文本视图现在已经丢失了。有谁知道为什么??
继承我的代码:
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
RelativeLayout.LayoutParams layout1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout1.setMargins(0, 0, 0, 0); //(left, top, right, bottom)
layout1.addRule(RelativeLayout.CENTER_IN_PARENT);
RelativeLayout.LayoutParams layout2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout2.setMargins(50, 50, 0, 0); //(left, top, right, bottom)
layout2.addRule(RelativeLayout.CENTER_IN_PARENT);
t1.setLayoutParams(layout1);
t2.setLayoutParams(layout2);
relativeLayout.addView(t1);
relativeLayout.addView(t2);
row.addView(relativeLayout);
tableView.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
继承我的布局代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/ourTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7.64" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>