2013-05-23 5 views
19

TextView属性の意味はわかりません。layout_alignWithParentIfMissingの意味は分かりません。layout_alignWithParentIfMissingの理解が難しい

私は、次のdocumentationをお読みください。

android:layout_alignWithParentIfMissing

trueに設定するアンカーがlayout_toLeftOfのために見つけることができない場合、親はアンカーとして使用され、layout_toRightOfなど

ブール値のいずれかでなければならず、trueまたはfalseのいずれかです。

私に説明してください。

答えて

24

これは、RelativeLayoutを使用する場合にのみ適用されます。

要素を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> 

この例を見てみましょうあなたはtextView1を削除すると何が起こるかを参照してくださいが、textView2に手を触れないでください。 (それは親の内側にあるので、右ではない)

それは

Androidのではなく、それが欠落しているので、それがあるべき要素の親要素を位置決めします左に、下に移動します。

falseの場合、欠落要素に関するパラメータは無視されます。

+0

TextViewのレイアウトパラメータが無効です: layout_alignWithParentIfMissing - このエラーは発生しています – vhd

+0

うん!出来た :) – vhd

0

相対レイアウトでは、ビューの位置を別のビューに対して相対的に指定できます。

タグlayout_toLeftOflayout_toRightOf位置「アンカー」ビュー・オブジェクトを視野に相対。このアンカービューが見つからない場合、のalignWithParentIfMissingというタグは、ビューを親ビューと整列させます。

2

別のサンプル。私はちょうど使用しました。

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

"streamOverflowは、" 欠落している場合は、 "delete_time" のTextViewは、アンドロイドを持つように振る舞う:layout_alignParentRight = "true" のプロパティ。