为什么我的xml中的layout_gravity中只有一个属性正常工作

时间:2015-12-30 18:30:30

标签: android xml layout

我希望我的重置按钮位于底部并水平居中,但layout_gravity不起作用,只有其中一个正常工作。

我知道可以通过选择RelativeLayout作为我的父布局来完成,但我想用LinearLayout作为我的父布局。

我只想把重置按钮作为底部和水平居中的休息一切都很好。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.courtcounter.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="TEAM A" />

        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="button3"
            android:text="+3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="button2"
            android:text="+2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="button1"
            android:text="Free Throw" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="TEAM B" />

        <TextView
            android:id="@+id/team_b_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="0" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="button3_b"
            android:text="+3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="button2_b"
            android:text="+2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="button1_b"
            android:text="Free Throw" />
    </LinearLayout>
</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:onClick="resetButton"
    android:padding="8dp"
    android:text="RESET" />

</LinearLayout>

screenshot

2 个答案:

答案 0 :(得分:0)

如果您不想完全相对(在父线性布局内),请使用此选项:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:onClick="resetButton"
        android:padding="8dp"
        android:text="RESET"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

或者如果您不想将重置按钮保留在相对布局中,似乎没有解决方案。

您也可以查看此内容:How to align views at the bottom of the screen?

答案 1 :(得分:0)

由于该布局中的其余ViewGroups填充了父级的宽度,您可以简单地将父级LinearLayout的重力设置为center_horizo​​ntal,它应该正确排列,而不会影响其他子项:

<LinearLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context="com.example.android.courtcounter.MainActivity">

编辑:我没有注意到你也想在底部...这可以通过你的内容和底部之间的隐形加权视图来实现:

    ...

    <View
        android:layout_width="1dip"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="resetButton"
        android:padding="8dp"
        android:text="RESET" />

</LinearLayout>

编辑2:删除了按钮上的layout_gravity,因为它不再适用。