2012-04-23 53 views
0

我的表由动态数据填充,所以我在代码中添加TableRows及其视图。 TableLayout和第一行(我的头文件)是在xml中构建的。重力集中在这些项目上工作。但是将Gravity设置为以我动态创建的TableRows和Views为中心不起作用。TextView Gravity不能在TableLayout中工作

<TableLayout 
    android:id="@+id/poweredEquip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:gravity="center" 
    android:padding="10dip">   
    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/tab_bg_unselected" 
     android:gravity="center"> 
     <TextView 
     android:text="Serial" 
     android:textSize="15sp" 
     android:textColor="@android:color/black" 
     android:layout_width="130px" 
     android:layout_height="wrap_content" 
     android:ellipsize="middle" 
     android:gravity="center" 
     android:layout_column="1"/> 
     <TextView 
     android:text="Model" 
     android:textSize="15sp" 
     android:textColor="@android:color/black" 
     android:layout_width="110px" 
     android:layout_height="wrap_content" 
     android:ellipsize="middle" 
     android:gravity="center" 
     android:layout_column="2"/> 
     <TextView 
     android:text="Status" 
     android:textSize="15sp" 
     android:textColor="@android:color/black" 
     android:layout_width="150px" 
     android:layout_height="wrap_content" 
     android:ellipsize="middle" 
     android:gravity="center" 
     android:layout_column="3"/> 
     <TextView 
     android:text="Tool Turned" 
     android:textSize="15sp" 
     android:textColor="@android:color/black" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:ellipsize="middle" 
     android:gravity="center" 
     android:layout_column="4"/> 
     <TextView 
     android:text="Hours" 
     android:textSize="15sp" 
     android:textColor="@android:color/black" 
     android:layout_width="110px" 
     android:layout_height="wrap_content" 
     android:ellipsize="middle" 
     android:gravity="center" 
     android:layout_column="5"/> 
     <TextView 
     android:text="Trailer Model" 
     android:textSize="15sp" 
     android:textColor="@android:color/black" 
     android:layout_width="110px" 
     android:layout_height="wrap_content" 
     android:ellipsize="middle" 
     android:gravity="center" 
     android:layout_column="6"/> 
    </TableRow> 
<TableLayout> 

Activity.cs

TableLayout powered = (TableLayout) FindViewById(Resource.Id.poweredEquip); 
TableRow.LayoutParams p = new TableRow.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent) {Gravity = GravityFlags.Center}; 
TableRow.LayoutParams p1 = new TableRow.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent) {Column = 1, Gravity = GravityFlags.Center}; 

TableRow row = new TableRow(this) { LayoutParameters = p }; 
text = new TextView(this) {Text = dr["Serial"].ToString(), LayoutParameters = p1}; 
text.SetPadding(8, 8, 8, 8); 
text.SetMaxWidth(150); 
text.SetMinWidth(150); 
row.AddView(text); 

powered.AddView(row); 
+0

为文字显示出来? – JRaymond 2012-04-23 17:29:14

+0

是的。除了对齐,一切都可以工作 – jmease 2012-04-23 18:24:36

回答

2
  1. 你可以发布你的XML布局的文本视图(只有1或2)的工作?

  2. 刺在黑暗中,但你可能想尝试设置LAYOUT_GRAVITY文本的布局参数,而不是重力的,因为你是迫使宽度是150指定布局比重为中心应该强制所有的TextView到移动到父容器的中间(表格行)。

  3. 另一个想法:当您为TextView设置布局参数时,它使用TableRow.LayoutParams。我想认为它可能实际上是参照表格行设置参数,而不是设置TextView本身的参数。尝试调用此对您创建的每个TextView的将它添加到表行之前:

    text.setGravity(Gravity.CENTER);

+0

将一些TextView添加到原始代码。我没有看到Layout_Gravity作为TableRow.LayoutParams的成员。那会是什么? – jmease 2012-04-23 18:24:08

+0

看起来好像没有在代码背后的textview中设置layout_gravity的正确方法。你能设置textview的(在代码后面)有一些背景,并设置表格行有不同的彩色背景,让它运行并截图?这会给我们一些想法,如何在表格行中设置标签(重力,大小)。 – Gophermofur 2012-04-23 19:01:45

+0

我将TextView背景设置为橙色,并且无论文本的长度如何,TextView占据了整个列的宽度。文本总是与左边对齐。 – jmease 2012-04-23 19:10:25

0

如果你想为你的tablerows相同的属性,那么你可以很容易地做到这一点。

只需从xml布局中使用get layout param,然后将这些layoutparmas设置为您的动态表格行。

xml_table_row =(的TableRow)findviewbyid(R.id.xml_table_row_id) TableLayout.layoutparam PARAM = xml_table_row.getLayoutParams();

powered.AddView(row,param);

希望它能帮助你。如果没有请回复,以便我可以尝试其他方式。