为什么在Android XML中使用android:layout_gravity="right"无法生效?

4

我XML代码中ListView的行为:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

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

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2"
        android:gravity="center" >

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/app_icon_17"
            android:contentDescription="@string/empty" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="2"
        android:gravity="center" >

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/btn_delete_new"
            android:focusable="false" />
    </LinearLayout>

</LinearLayout>

我希望最后一个id为btn_delete的按钮保持右对齐。在设计时,"图形布局"中显示的正是我想要的效果。但当我运行它时,在模拟器上无法实现这个效果。
请看输出: enter image description here 那么为什么删除按钮不能右对齐呢?

2
我认为如果你使用 RelativeLayout 或者 FrameLayout,它们都比 LinearLayout 更好,你就可以得到你想要的效果。我更喜欢 RelativeLayout,因为它适用于这种类型的列表行设计。 - user370305
1
为你的按钮添加layout_gravity并试一下! - Gridtestmail
13
由于水平线性布局忽略了layout_gravity属性中的right、left和horizontal center,它只关心垂直方向上的gravity属性。 - njzk2
@njzk2:你提到的是一个显而易见但很容易被忽略的事情。解决了我的问题。谢谢! - Yash
5个回答

8
使用相对布局(relative layout)来实现此功能。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_name"
            android:layout_marginLeft="10dp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/btn_delete_new"
            android:focusable="false" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_toLeftOf="@+id/rl_btn" >

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/app_icon_17"
            android:contentDescription="@string/empty" />
    </RelativeLayout>

</RelativeLayout>

5

您可能误解了布局,那些嵌套的LinearLayouts并不需要。使用RelativeLayout,例如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp" />

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/tv_time"
        android:background="@drawable/app_icon_17"
        android:contentDescription="@string/empty" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/btn_delete_new"
        android:focusable="false" />

</RelativeLayout>

我应该读些什么来理解它们?并且理解权重。 - siriuseteor77

1

正如我们所看到的,您已经为您的行布局分配了权重。

如果您为您的父布局分配了权重,则只需相应地设置高度或宽度为 0dp 即可。 在您的情况下android:layout_width =“0dp”

但是当您有该加权布局的子布局时,您必须将那些布局的属性设置为 fill_parent ,即:按钮的android:layout_height =“fill_parent”

因此,如果您不想更改父布局并想使用现有代码进行工作,则可以尝试以下代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

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

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        >

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:background="@drawable/icon"
            android:contentDescription="@string/app_name" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2" >

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:background="@drawable/icon"
            android:focusable="false" />

    </LinearLayout>

</LinearLayout>

0

更改这个

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="2"
        android:gravity="center" >

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/btn_delete_new"
            android:focusable="false" />
    </LinearLayout>

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="right"
            android:background="@drawable/btn_delete_new"
            android:focusable="false" />
    </LinearLayout>

你正在将 android:layout_gravity="right" 应用于布局。相反,你应该将其应用于布局内的按钮。我认为你不需要为该布局设置权重2,但如果你需要可以添加它。希望这能帮到你。

@ShirishHerwade 欢迎。 :) - MysticMagicϡ
抱歉。我已经尝试回答了,但是没有得到任何响应。 - Ganesan J

-3
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="right"
        android:gravity="right"
        android:background="@drawable/btn_delete_new"
        android:focusable="false" />
</LinearLayout>

1
这个答案代表什么? - IgorGanapolsky
1
只有代码没有解释很少有帮助。 - NoChinDeluxe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接