0

私のテーブルには動的データが入力されているので、コードにTableRowsとそのビューを追加しています。TableLayoutと最初の行(私のヘッダー)はxmlに組み込まれています。重力を中心に設定すると、これらのアイテムに作用します。しかし、動的に作成されたTableRowsとViewsを中心とするようにGravityを設定しても機能しません。

<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);
4

2 に答える 2

2
  1. 動作するテキストビュー(1つまたは2つ)のXMLレイアウトを投稿できますか?

  2. 暗闇の中で突き刺しますが、幅を150に強制しているため、GRAVITYではなくテキストのレイアウトパラメータにLAYOUT_GRAVITYを設定してみてください。レイアウト重力をCENTERとして指定すると、TEXTVIEW全体が中央に移動します。親コンテナ(テーブル行)。

  3. 別の考え:TextViewのレイアウトパラメータを設定するときは、TableRow.LayoutParamsを使用しています。TextView自体のパラメータを設定するのではなく、実際にはテーブルの行を参照してパラメータを設定しているのではないかと思います。テーブルの行に追加する前に、作成する各TextViewでこれを呼び出してみてください。

    text.setGravity(Gravity.CENTER);

于 2012-04-23T17:36:33.223 に答える
0

テーブル行に同じプロパティが必要な場合は、簡単に実行できます。

xmlレイアウトからgetlayoutparamを使用して、それらのlayoutparmasを動的テーブル行に設定するだけです。

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

Powered.AddView(row、param);

それがあなたを助けることを願っています。そうでない場合は、別の方法を試すことができるように応答してください。

于 2012-04-23T17:33:48.270 に答える