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

时间:2013-01-07 10:09:42

标签: android android-layout android-linearlayout gravity 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>

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

见输出:

enter image description here

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

5 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

答案 2 :(得分: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>

答案 3 :(得分: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的布局。但是如果它需要你可以添加它。希望它有所帮助。

答案 4 :(得分:-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>