Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difficulty understanding layout_alignWithParentIfMissing

I don't understand the meaning of layout_alignWithParentIfMissing for the TextView attribute.

I read the following documentation:

android:layout_alignWithParentIfMissing

If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc.

Must be a boolean value, either true or false.

Please explain to me.

like image 442
vhd Avatar asked May 23 '13 07:05

vhd


2 Answers

This apply only when using RelativeLayout.

If You set element to be toLeftOf some other element it means it will be on the left of this element.

But if this element will be missing, because You delete it for example it will be align to parent.

Take this example

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

See what happens If You delete textView1 but don't touch textView2.

It will go to the bottom, on left(not right because it is inside of parent)

Android will position element relative to parent instead of element it should be because it is missing.

If it is false parameters relating to missing elements will be ignored.

like image 172
Gustek Avatar answered Oct 27 '22 09:10

Gustek


Another sample. I have just used.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="6dip" >

    <Button
       android:id="@+id/streamOverflow"
       android:layout_width="50dp"
       android:layout_height="30dp"
       android:layout_alignParentRight="true"
       android:background="@drawable/ic_action_overflow"
       android:focusable="false" />

    <TextView
       android:id="@+id/delete_time"
       style="?android:attr/textAppearanceSmall"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Disappears "
       android:layout_toLeftOf="@id/streamOverflow"
       android:layout_alignWithParentIfMissing="true"
     />
</RelativeLayout>

if "streamOverflow" is missing, "delete_time" textview behaves like having android:layout_alignParentRight="true" property.

like image 28
faraway Avatar answered Oct 27 '22 08:10

faraway