【问题标题】:Button does not want to go right in TableLayout按钮不想在 TableLayout 中向右移动
【发布时间】:2011-06-09 14:51:08
【问题描述】:

这段代码有什么问题?为什么按钮在此表格行中不正确?

<TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
        <TableRow>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/str1"
                    />
            <EditText
                    android:id="@+id/id1"
                    android:layout_width="70dp"
                    android:layout_height="wrap_content"
                    />
            <Button
                    android:id="@+id/id2"
                    android:layout_width="wrap_content"
                    android:layout_height="65dp"
                    android:text="@string/str2"
                    android:gravity="right"/>
        </TableRow>
</TableLayout>

【问题讨论】:

  • 卡在EditText(右侧)。

标签: android tablelayout tablerow gravity


【解决方案1】:

您应该在 TableLayout 中使用 android:stretchColumns="1",以便 EditText 将您的 Button 推到右侧。

或者您可以在 tableRow 中的视图上使用 android:layout_weight 属性来相应地分配空间。

另外,您不需要在 TableRow 中的视图上指定宽度和高度属性。

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:stretchColumns="1">
        <TableRow>
            <TextView
                android:text="@string/str1"
                />
            <EditText
                android:id="@+id/id1"
                 />
            <Button
                android:id="@+id/id2"
                android:text="@string/str2"
                />
        </TableRow>
    </TableLayout>

【讨论】:

  • android:stretchColumns="1" 丢失。谢谢
【解决方案2】:

android:gravity="right" 用于Button 中的文本。由于它的宽度设置为wrap_content,所以完全没有效果。

您需要改用android:layout_gravity。见:http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity

子代可以提供给父代的标准重力常数。定义如何在其父视图组中放置视图,包括其 x 轴和 y 轴。

必须是以下常量值中的一个或多个(用“|”分隔)。

(...)

right 0x05 将对象推到其容器的右侧,不改变其大小。

【讨论】:

  • 问题是我已经尝试了两种重力属性,但都没有给我任何结果。我觉得下贴是对的:我忘了stretchColumns这个属性了。
  • 这是罗马尼亚人所说的(可能对某人有所帮助):gravity means "apply gravity to the content of this view" whereas layout_gravity means "apply gravity to this view within its parent." So on a TextView, gravity will align the text within the bounds of the TextView whereas layout_gravity will align the TextView within the bounds of its parent.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多