カスタム複合コントロールで xml を膨張させました。
public class CustomView extends RelativeLayout{
private LayoutInflater inflater;
public CustomView(Context context) {
super(context);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView(inflater.inflate(R.layout.common_views, null));
}
}
この複合コントロールを 4 つの異なるレイアウトに含めました。レイアウトの 1 つ:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/wall1Layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/wall1withkey" >
<com.example.room.CustomView
android:id="@+id/customView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
....
</RelativeLayout>
viewflipper に含めた 4 つのレイアウトすべて:
<ViewFlipper android:id="@+id/water_room_flipper"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/water_room_wall1" android:id="@+id/first" />
<include layout="@layout/water_room_wall2" android:id="@+id/second" />
<include layout="@layout/water_room_wall3" android:id="@+id/third" />
<include layout="@layout/water_room_wall4" android:id="@+id/fourth" />
</ViewFlipper>
次に、メイン アクティビティのメソッドで、実行時にこの複合コントロールのいくつかのコントロールを変更しました。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.water_room_view_flipper);
vf = (ViewFlipper) findViewById(R.id.water_room_flipper);
vf.setDisplayedChild(R.id.first);
}
...
public void someOnClickMethod(){
...
RelativeLayout rl = (RelativeLayout) findViewById(R.id.customView);
ImageButton newItem = (ImageButton)rl.findViewById(R.id.ImageButton01);//this is inside common_view.xml which was inflated in CustomView class(compound countrol)
newItem.setBackgroundResource(R.drawable.key_stored);
}
ご覧のとおり、 someOnClickMethod はコントロールの 1 つの背景画像を変更します。ただし、他の 3 つのレイアウトでは有効ではなく、最初のレイアウトでのみ有効です。複合コントロールが含まれるすべてのレイアウトで有効になるようにコントロールを変更するにはどうすればよいですか?