【问题标题】:why android:layout_gravity="right" not working here in android xml为什么 android:layout_gravity="right" 在 android xml 中不起作用
【发布时间】:2012-12-21 01:13:36
【问题描述】:

我的列表视图行的 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 的最后一个按钮保持右对齐。并且它在设计时在“图形布局”中显示为我想要的。但是当我运行它时,在模拟器上它不起作用。

查看输出:

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

【问题讨论】:

  • 我认为如果你使用RelativeLayoutFrameLayout,那么两者都比LinearLayout 更好,你会得到你想要的。我更喜欢RelativeLayout,就像这种类型的列表行设计..
  • 为您的按钮添加 layout_gravity 并试用!
  • 因为水平线性布局忽略了layout_gravity右、左和水平居中。它只关心垂直重力。
  • @njzk2 :你提到的一个明显但很容易错过的事情。解决了我的问题。谢谢!

标签: android android-layout android-linearlayout gravity layout-gravity


【解决方案1】:

使用这个,使用相对布局

<?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>

【讨论】:

    【解决方案2】:

    您似乎误解了布局,不需要那些嵌入的 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>
    

    【讨论】:

    • 我可以阅读什么来理解它们?并了解权重
    【解决方案3】:

    我们可以看到,您已为行布局分配权重。

    如果您为父布局分配权重,您只需设置相应的高度或使用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>
    

    【讨论】:

      【解决方案4】:

      改变这个

      <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 欢迎您。 :)
      【解决方案5】:
      <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>
      

      【讨论】:

      • 这个答案代表什么?
      • 没有解释的代码很少有用。
      猜你喜欢
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多