1

我在我的自定义复合控件中膨胀了 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 种不同的布局中包含了这个复合控件。布局之一:

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

然后我在mainactivity的一个方法中在运行时更改了这个复合控件的一些控件:

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 更改了其中一个控件的背景图像。但它不会在其他 3 种布局中生效,仅在第一种中生效。如何更改控件以使其在包含复合控件的所有布局中生效?

4

0 回答 0