我想在运行时将TextView添加到GridLayout。由于没有属性可以设置GridLayout的单元格背景,所以我将TextView Background设置为单元格背景,在其边框处有笔触。我的问题是,在运行时将TextView添加到Gridlayout时,TextView大小不等于GridLayout单元格。拉伸TextView宽度和高度等于GridLayout的单元格宽度高度
在XML文件时,我设置:
<GridLayout
android:id="@+id/gridLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignmentMode="alignBounds"
android:columnCount="5"
android:columnOrderPreserved="false"
android:useDefaultMargins="false" >
<TextView
android:layout_gravity="fill"
android:background="@drawable/grid_lay_cell_bg"
android:padding="5dp"
android:text="Name"
android:textSize="14dp" />
</GridLayout>
它的工作完美,TextView的覆盖完整的单元尺寸,但是当我试图将它在下面的代码运行时间:
TextView tv3 = new TextView(getActivity());
GridLayout.LayoutParams lp3 = new GridLayout.LayoutParams(GridLayout.spec(2, 1), GridLayout.spec(0, 1));
lp3.setGravity(Gravity.FILL);
tv3.setLayoutParams(lp3);
tv3.setGravity(Gravity.FILL);
tv3.setPadding(padd, padd, padd, padd);
tv3.setBackgroundResource(R.drawable.grid_lay_cell_bg);
tv3.setTextSize(16);
tv3.setText("Test");
gridLay.addView(tv3, lp3);
的TextView不涵盖完整的细胞大小。
请给我一些提示,如何在运行时在Android中将TextView添加到GridLayout时,将TextView大小拉伸到与单元大小相等。
退房:http://stackoverflow.com/questions/20871690/dont-understand-how-to-use-gridlayout-spec –
@ Haresh Chhelana。 ..谢谢你的回复,但这不是我要求 – mark