ビューを xml から対応するビュー オブジェクトに拡張し、これらのビュー オブジェクトをプログラムされたビューグループに追加しようとしています。ただし、myviewgroup.addView(childview) を呼び出すたびに、childview はすべてのボタンと xml コードが指定する必要のあるものを失うようです。ただし、childview が持っていることの 1 つは、childview に正しい寸法が与えられるようにプログラムした背景色です。childview には正しい寸法が指定されていますが、そのコンポーネントは表示されません
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewchanger= new ViewChanger(this); //my viewgroup
View mainmenu= inflater.inflate(R.layout.main, null); //the xml code
mainmenu.setBackgroundColor(Color.rgb(0, 0, 255));
viewchanger.addView(mainmenu);
setContentView(viewchanger);
上記のコードは、画面の背景を青に設定するだけですが、ボタンは表示されません。また、setContentView(mainmenu) を設定すると、その xml コードから背景色だけでなくすべてが完全に表示されます。このビューグループのポイントは、子ビューとしての順序に関係なく、複数のビューを切り替えることができるようにすることです。
ビューチェンジャーのコードは次のとおりです。7 つの子ビューを持たせたいのですが、メソッド setView(int v) を呼び出すと、他のすべてのビューがなくなり、その 1 つのビューだけが表示されるようになります。
public class ViewChanger extends ViewGroup{
/**Represent which view to show, only one can show at a time*/
public static final int MainMenu=0, ChooseMap=1, MapOptions=2,
SetLocation=3, view=4, EditMap=5, CreateMap=6;
private int current; //the current view
private View[] views= new View[7];
public ViewChanger(Context context) {
super(context);
//load 7 views into view array here
//LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*views[0]= inflater.inflate(R.layout.main, null);
* views[1]= inflater.inflate(R.layout.choose, null);
* views[2]= inflater.inflate(R.layout.mapoptions, null);
* views[0].setBackgroundColor(Color.rgb(255, 0, 0));
* views[1].setBackgroundColor(Color.rgb(255, 255, 0));
* views[2].setBackgroundColor(Color.rgb(0, 255, 0));*/
* addView(views[0]);
* addView(views[1]);
* addView(views[2]);
* this is how i want to load the childviews*/
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for(int i = 0; i < getChildCount(); i++){
getChildAt(i).layout(0, 0, getWidth(), getHeight());
}
}
/**Sets the view of the view group, only one view can be viewed at a time*/
public void setView(int v){
current=v;
for (int i=0; i<views.length; i++){
if (v==i){
views[i].setVisibility(View.VISIBLE);
} else{
views[i].setVisibility(View.GONE);
}
}
}
ViewChanger コンストラクターで子ビューを読み込むと、ビューグループには、setView(int v) で設定したビューの背景色のみが表示され、ボタンは表示されません。
main の xml コードを次に示しますが、他のすべてはこれと非常によく似ています。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="The title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome [...]"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/load"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Load Map" />
<Button
android:id="@+id/create"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Create Map" />
<Button
android:id="@+id/users"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:text="Other User" />
</LinearLayout>