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

Ely*_*lye 2 android android-layout android-relativelayout

如果这是不同的 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>
Run Code Online (Sandbox Code Playgroud)

下面显示了 v19 SDK 显示的内容。例如,蓝色与绿色框对齐,这是由于android:layout_alignBottom="@id/img_picture" v18

但是,对于 v18 SDK 及之前(通过模拟器和真实设备在 v17 和 v16 上测试),图像是蓝色框未按预期对齐的布局,但对齐扩展到@+id\img_picture.

v19

如果这是 Android v18 SDK 问题,或者我错过了什么,有人可以解释一下。谢谢!

小智 5

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

如果你想从他的父母那里添加一个边距,试试这个:

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

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