android:layout_alignBottom 不适用于 API 18 岁及以下?

android:layout_alignBottom not working for API 18 and below?

如果这是不同的 Android 版本问题(v18 SDK 及之前),需要一些启发。

下面是我们的布局,特别是 txt_subitem 应该与 img_picture 底部对齐。下面的代码适用于 v19 及更高版本(自 Kit-Kat 起),但不适用于 v18,如下图所示。

<RelativeLayout
    android:id="@+id/img_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img_picture"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:src="#0f0"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_margin="20dp"/>

    <TextView
        android:id="@+id/txt_subitem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/img_picture"
        android:text="testing"
        android:layout_alignBottom="@+id/img_picture"
        android:background="#00f"
         />

</RelativeLayout>

下图为v19 SDK展示的内容。例如。蓝色与绿色框对齐,这是预期的,因为 android:layout_alignBottom="@id/img_picture"

但是,对于 v18 SDK 及之前版本(通过模拟器和真实设备在 v17 和 v16 上进行测试),图像布局中蓝色框未按预期对齐,但对齐延伸到边距 (20dp) @+id\img_picture

如果这是 Android v18 SDK 问题或者我遗漏了什么,有人可以解释一下吗?谢谢!

对于 API 18 及更早版本,边距在对齐后应用,因此如果 ImageView 中的边距大于 0,您将移动文本视图。您可以检查此设置边距为 0。

如果你想从他的 parent 中添加边距,试试这个:

  • 保持 android:layout_margin="20dp",将 android:layout_marginTop="10dp" 添加到您的 TextView
  • 不保留 android:layout_margin="20dp",在 parent 视图中使用填充(如示例)。

    <RelativeLayout
        android:id="@+id/img_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp">
    
        <ImageView
            android:id="@+id/img_picture"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:src="#0f0"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
    
        <TextView
            android:id="@+id/txt_subitem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/img_picture"
            android:layout_alignLeft="@id/img_picture"
            android:text="testing"
            android:background="#00f"/>
    
    </RelativeLayout>
    

@由 Frank N. Stein 提供

  <RelativeLayout
        android:id="@+id/img_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/img_picture"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:src="#0f0"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="20dp"/>

        <TextView
            android:id="@+id/txt_subitem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/img_picture"
            android:text="testing"
            android:layout_alignBottom="@id/img_picture"
            android:background="#00f"

            />

    </RelativeLayout>

您可以使用 FrameLayout 而不是 RelativeLayout。这将适合您的 requirement.In FrameLayout 您可以简单地使用重力移动您的 TextView。