在运行时更改复合控件的控件

时间:2013-10-19 20:20:24

标签: android android-layout custom-controls

我在自定义复合控件中夸大了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种布局中生效,仅在第一种布局中生效。如何更改控件以使其在复合控件中包含的所有布局中生效?

0 个答案:

没有答案