难以理解layout_alignWithParentIfMissing

vhd*_*vhd 20 android android-layout

我不明白的意思layout_alignWithParentIfMissingTextView属性.

我阅读了以下文档:

android:layout_alignWithParentIfMissing

如果设置为true,则父将作为锚时,锚不能被用于发现layout_toLeftOf,layout_toRightOf等等.

必须是一个布尔值,无论是truefalse.

请向我解释.

Gus*_*tek 26

这仅适用于使用RelativeLayout时.

如果将element设置为toLeftOf,则表示它将位于此元素的左侧.

但是如果这个元素丢失了,因为你删除了它,例如它将与父对齐.

举个例子

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="102dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignWithParentIfMissing="true"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

看看会发生什么如果删除textView1但不要触摸textView2.

它会在左边的底部(不正确,因为它在父母的内部)

Android会将元素相对于父元素而不是元素定位,因为它缺失了.

如果是假,则将忽略与缺失元素相关的参数.