【问题标题】:TextView Gravity Not Working in TableLayoutTextView 重力在 TableLayout 中不起作用
【发布时间】:2012-04-24 03:12:56
【问题描述】:

我的表格由动态数据填充,因此我在代码中添加了 TableRows 及其视图。 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);

【问题讨论】:

  • 是的。除了对齐之外,一切正常。

标签: android xamarin.android


【解决方案1】:
  1. 你能发布你的文本视图(只有 1 个或 2 个)的 XML 布局吗?

  2. 在黑暗中刺穿,但您可能想尝试为文本的布局参数设置 LAYOUT_GRAVITY 而不是 GRAVITY,因为您将宽度强制为 150。将 Layout Gravity 指定为 CENTER 应该会强制整个 TEXTVIEW 移动到父容器的中间(表格行)。

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

    text.setGravity(Gravity.CENTER);

【讨论】:

  • 在原始代码中添加了一些 TextViews。我没有将 Layout_Gravity 视为 TableRow.LayoutParams 的成员。那会是什么?
  • 在后面的代码中似乎没有在 textview 上设置 layout_gravity 的正确方法。您可以将文本视图(在后面的代码中)设置为具有一些背景并将表格行设置为具有不同颜色的背景,让它运行并截取屏幕截图吗?这将使我们了解如何在表格行中设置标签(重力、大小)。
  • 我将 TextView 背景设置为橙色,看起来无论文本长度如何,TextView 都占据了列的整个宽度。文本总是左对齐。
  • 在这种情况下,请参阅我上面编辑过的帖子。文本实际上可能没有在 textview 中居中。
  • 在 TextView 上设置重力修复了它。谢谢!
【解决方案2】:

如果您希望表格行具有相同的属性,那么您可以轻松完成。

只需从您的 xml 布局中获取布局参数,然后将这些布局参数设置为您的动态表格行。

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

powered.AddView(row, param);

希望对您有所帮助。如果不是,请回复,以便我可以尝试其他方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多