2016年1月19日火曜日

android:baselineAlignedChildIndex の振る舞い

LinearLayout が baselineAligned されている他のレイアウトの子であるとき、android:baselineAlignedChildIndex で baseline に対応させる子ビューを指定することができます。

まず、以下のようなレイアウトがあった場合
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal">  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#ccccff"  
  11.         android:paddingTop="60dp"  
  12.         android:text="Cupcake" />  
  13.   
  14.     <LinearLayout  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_marginLeft="10dp"  
  18.         android:layout_marginRight="10dp"  
  19.         android:background="#ffcccc"  
  20.         android:orientation="vertical">  
  21.   
  22.         <TextView  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="Donuts" />  
  26.   
  27.         <TextView  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:text="Eclare" />  
  31.   
  32.     </LinearLayout>  
  33.   
  34. </LinearLayout>  
実行結果はこのようになります。



LinearLayout はデフォルトで android:baselineAligned="true" がセットされているので、子ビューの LinearLayout に android:baselineAlignedChildIndex="0" を指定すると次ようなレイアウトに変わります。
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     ...>  
  4.   
  5.     <TextView  
  6.         ... />  
  7.   
  8.     <LinearLayout  
  9.         ...  
  10.         android:baselineAlignedChildIndex="0">  
  11.   
  12.         ....  
  13.   
  14.     </LinearLayout>  
  15.   
  16. </LinearLayout>  


android:baselineAlignedChildIndex="1" を指定すると次のようになります。




0 件のコメント:

コメントを投稿