android:duplicateParentState="true or false"
setDuplicateParentStateEnabled(boolean)
が使えます。
これを設定すると、自身の状態よりも親の状態が優先され、親の状態と同じ状態をとるようになります。
例えば、ダッシュボードで
(ボタン1)
-----------
|画像|画像|
-----------
(ボタン2)
-----------
|画像|画像|
-----------
(ボタン3)
-----------
|画像|画像|
-----------
・・・
があって、どっちの画像を押しても、両方の画像が状態に応じて変わるようにするには、両方の画像の親Viewにクリック処理をさせ、子View に android:duplicateParentState="true" を設定します。
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:clickable="true"
- android:focusable="true"
- >
- <ImageView
- android:background="@android:drawable/btn_default"
- android:layout_width="50dip"
- android:layout_height="wrap_content"
- android:duplicateParentState="true"
- />
- <ImageView
- android:background="@android:drawable/btn_default"
- android:layout_width="100dip"
- android:layout_height="wrap_content"
- android:duplicateParentState="true"
- />
- </LinearLayout>
- ...
この場合、LinearLayout 部分をクリックすると、2つの ImageView 両方が LinearLayout の状態(押された、フォーカスがあたったなど)に応じて変わります。

ちなみに、これを使ったダッシュボードの例が「Android Layout Cookbook」の 8.4 レイアウトサンプル4 です。
ここから、Android Layout Cookbook デモあぷりを落として実際の動きをみることができます。
Android Layout CookBook デモあぷり - Android マーケット -
本もよろしくね!
実は、親 View の enabled/disabled も伝えることができます。
例えば、
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/container"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:text="Button"
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:duplicateParentState="true"
- />
- <Button
- android:text="Button"
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:duplicateParentState="false"
- />
- <Button
- android:text="Button"
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:duplicateParentState="true"
- />
- <Button
- android:text="Button"
- android:id="@+id/button4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:duplicateParentState="true"
- />
- <Button
- android:text="Button"
- android:id="@+id/button5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:duplicateParentState="true"
- />
- </LinearLayout>
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- View container = findViewById(R.id.container);
- container.setEnabled(false);
- View button4 = findViewById(R.id.button4);
- button4.setEnabled(true);
- View button5 = findViewById(R.id.button5);
- button5.setDuplicateParentStateEnabled(false);
- button5.setEnabled(true);
- }
の結果は次のようになります。

setDuplicateParentStateEnabled(false) を指定しないと、子 View だけ setEnabled(true) しないと親 View が disabled の場合は enabled になりません。
このパラメータは一度に有効/無効を切り替えるのに使うにはちょっと向いてないかなー。
0 件のコメント:
コメントを投稿