2015-05-01 195 views
0

我想在运行时将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大小拉伸到与单元大小相等。

enter image description here

+0

退房:http://stackoverflow.com/questions/20871690/dont-understand-how-to-use-gridlayout-spec –

+0

@ Haresh Chhelana。 ..谢谢你的回复,但这不是我要求 – mark

回答

0

试试这个

TextView textView = (TextView)findViewById(R.id.text_view); 
ViewGroup.LayoutParams params = textView.getLayoutParams(); 
params.width = ViewGroup.LayoutParams.FILL_PARENT; 
textView.setLayoutParams(params); 
+0

我已经试过这个: GridLayout.LayoutParams lp = new GridLayout.LayoutParams(); \t \t \t lp.width = LayoutParams.FILL_PARENT; \t \t \t lp.height = LayoutParams.FILL_PARENT; \t \t \t lp.setGravity(Gravity.FILL); tv.setLayoutParams(lp); 但没有运气:“( – mark

+0

显示屏幕截图你现在 –

+0

得到什么@穆尔塔扎胡尔希德·侯赛因我已经更新了我的问题,我的问题的补充截图 – mark