当前位置: 代码迷 >> Android >> 当API> 20时,RelativeLayout的最后一个ChildView无法看到
  详细解决方案

当API> 20时,RelativeLayout的最后一个ChildView无法看到

热度:73   发布时间:2023-08-04 09:48:52.0

这是我的代码:

<?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"
>
<Button
    android:text="button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    />
<TextView
    android:text="right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
     />
</RelativeLayout>

当API> 20时,TextView被按钮覆盖,如何解决?如果第一个孩子是TextView,则不会发生。这是android中的错误吗?

尝试这个,

    <?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"
        >
        <Button
            android:text="button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:layout_toLeftOf="@+id/tv_right"
            />
        <TextView
            android:id="@+id/tv_right"
            android:text="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            />
    </RelativeLayout>

您具有根布局match_parent的高度和宽度,并且正使用android:layout_height = match_parent通过按钮占据完整的空间

对于textview,您没有使用按钮的引用将其放置在布局上相对布局提供了android:layout_below属性,您可以使用此属性将textview放置在按钮下

检查以下示例将为您提供更多详细信息

<?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"
    >
    <Button
        android:text="button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:background="#ffffff"
        />
    <TextView
        android:text="right"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>

要突出显示:这不是android中的错误,您没有以正确的方式使用它

  相关解决方案