2011年7月6日水曜日

Android android:duplicateParentState

子View の状態(クリックされた、フォーカスがあたった、選択されたなど)を親と共有したい場合、

android:duplicateParentState="true or false"

setDuplicateParentStateEnabled(boolean)

が使えます。

これを設定すると、自身の状態よりも親の状態が優先され、親の状態と同じ状態をとるようになります。

例えば、ダッシュボードで

(ボタン1)
-----------
|画像|画像|
-----------
(ボタン2)
-----------
|画像|画像|
-----------
(ボタン3)
-----------
|画像|画像|
-----------
・・・

があって、どっちの画像を押しても、両方の画像が状態に応じて変わるようにするには、両方の画像の親Viewにクリック処理をさせ、子View に android:duplicateParentState="true" を設定します。

  1. <LinearLayout  
  2.     android:orientation="horizontal"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:clickable="true"  
  6.     android:focusable="true"  
  7.     >  
  8.     <ImageView   
  9.         android:background="@android:drawable/btn_default"  
  10.         android:layout_width="50dip"   
  11.         android:layout_height="wrap_content"  
  12.         android:duplicateParentState="true"  
  13.         />  
  14.     <ImageView   
  15.         android:background="@android:drawable/btn_default"  
  16.         android:layout_width="100dip"   
  17.         android:layout_height="wrap_content"  
  18.         android:duplicateParentState="true"  
  19.         />  
  20. </LinearLayout>  
  21.   
  22. ...  


この場合、LinearLayout 部分をクリックすると、2つの ImageView 両方が LinearLayout の状態(押された、フォーカスがあたったなど)に応じて変わります。



ちなみに、これを使ったダッシュボードの例が「Android Layout Cookbook」の 8.4 レイアウトサンプル4 です。
ここから、Android Layout Cookbook デモあぷりを落として実際の動きをみることができます。

Android Layout CookBook デモあぷり - Android マーケット -

本もよろしくね!



実は、親 View の enabled/disabled も伝えることができます。
例えば、

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/container"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8.     <Button   
  9.         android:text="Button"   
  10.         android:id="@+id/button1"   
  11.         android:layout_width="wrap_content"   
  12.         android:layout_height="wrap_content"  
  13.         android:duplicateParentState="true"  
  14.         />  
  15.     <Button   
  16.         android:text="Button"   
  17.         android:id="@+id/button2"   
  18.         android:layout_width="wrap_content"   
  19.         android:layout_height="wrap_content"  
  20.         android:duplicateParentState="false"  
  21.         />  
  22.     <Button   
  23.         android:text="Button"   
  24.         android:id="@+id/button3"   
  25.         android:layout_width="wrap_content"   
  26.         android:layout_height="wrap_content"  
  27.         android:duplicateParentState="true"  
  28.         />  
  29.     <Button   
  30.         android:text="Button"   
  31.         android:id="@+id/button4"   
  32.         android:layout_width="wrap_content"   
  33.         android:layout_height="wrap_content"  
  34.         android:duplicateParentState="true"  
  35.         />  
  36.     <Button   
  37.         android:text="Button"   
  38.         android:id="@+id/button5"   
  39.         android:layout_width="wrap_content"   
  40.         android:layout_height="wrap_content"  
  41.         android:duplicateParentState="true"  
  42.         />  
  43. </LinearLayout>  


  1. @Override  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.main);  
  5.       
  6.     View container = findViewById(R.id.container);   
  7.     container.setEnabled(false);  
  8.       
  9.     View button4 = findViewById(R.id.button4);  
  10.     button4.setEnabled(true);  
  11.   
  12.     View button5 = findViewById(R.id.button5);  
  13.     button5.setDuplicateParentStateEnabled(false);  
  14.     button5.setEnabled(true);  
  15. }  


の結果は次のようになります。



setDuplicateParentStateEnabled(false) を指定しないと、子 View だけ setEnabled(true) しないと親 View が disabled の場合は enabled になりません。

このパラメータは一度に有効/無効を切り替えるのに使うにはちょっと向いてないかなー。

0 件のコメント:

コメントを投稿