簡體   English   中英

TextView Gravity在TableLayout中不起作用

[英]TextView Gravity Not Working in TableLayout

我的表由動態數據填充,因此我在代碼中添加了TableRows及其Views。 TableLayout和第一行(我的標題)是在xml中構建的。 設置為居中的重力適用於這些項目。 但是將Gravity設置為我動態創建的TableRows和Views的中心不起作用。

<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);
  1. 你可以發布你的XML布局的textview(只有1或2)嗎?

  2. 在黑暗中刺,但您可能想嘗試為文本的布局參數而不是GRAVITY設置LAYOUT_GRAVITY,因為您強制寬度為150.將布局重力指定為CENTER應該強制整個TEXTVIEW移動到中間父容器(表行)。

  3. 另一個想法:當您為TextView設置布局參數時,它使用TableRow.LayoutParams。 認為實際上可能是參數表行設置參數,而不是設置TextView本身的參數。 嘗試在創建的每個TextView上調用它,然后將其添加到表行:

    text.setGravity(Gravity.CENTER);

如果你想為你的畫布提供相同的屬性,那么你可以輕松地完成它。

只需使用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);

希望它會對你有所幫助。 如果沒有,請回復,以便我可以嘗試另一種方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM