2

我想知道 android:layout_gravity 发生了什么。我的代码如下所示:

<Button
    android:id="@+id/test_button"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="test"
    android:textSize="25sp" />

但是该中心在按钮上绝对没有做任何事情,而 Eclipse 甚至在他们的下拉菜单中也没有。他们现在有另一种方式吗?

4

2 回答 2

2
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<Button
    android:id="@+id/test_button"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="test"
    android:textSize="25sp" />

</LinearLayout?

android:layout_gravityLinearLayout.LayoutParams

所以,它适用于线性布局。

或者像下面这样改变(不管父母是谁)

<Button
    android:id="@+id/test_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="test"
    android:textSize="25sp" />
于 2013-06-07T01:38:49.233 回答
1

你用的是什么布局?如果您的按钮在相对布局中,那可能是您的问题。您可以使用 LinearLayout,并使用 layout_gravity。或者在 RelativeLayout 中,您可以根据需要对齐视图。

于 2013-06-07T01:30:21.907 回答