为什么android:layout_gravity ="right"在android xml中不起作用

Shi*_*ade 4 android gravity android-layout android-linearlayout layout-gravity

我的列表视图行的xml代码是

    <?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>
Run Code Online (Sandbox Code Playgroud)

我希望id为btn_delete的最后一个按钮保持右对齐.并且它在设计时以"图形布局"的形式显示.但是当我运行它时,在模拟器上它不起作用.

看输出:

在此输入图像描述

那么为什么删除按钮没有正确对齐?

小智 8

使用此选项,使用相对布局

<?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>
Run Code Online (Sandbox Code Playgroud)


nmw*_*nmw 5

您似乎误解了布局,LinearLayouts不需要嵌入布局.使用a 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>
Run Code Online (Sandbox Code Playgroud)