2

ビューを 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>
4

3 に答える 3

6

考えられるすべてのことを精神的に実行した後、いくつかの可能性をチェックするために、これと同じダミー プロジェクトを作成しました。何らかの理由で、ViewGroup の代わりに FrameLayout から ViewChanger を拡張すると、これが機能します。おそらく、ViewGroup には子ビューをレンダリングするために必要なものがないためです (onDraw をオーバーライドしようとしましたが、それでも機能しませんでした)。一度に 1 つのビューしか表示していないため (FrameLayout の通常の使用法)、この変更を行うことをお勧めします。ViewGroup で欠落している描画機能を分離して修正するよりもはるかに簡単です。

public class ViewChanger extends FrameLayout {

元の設計 (コンストラクターからのビューの追加) を適切に機能させるために必要な変更は、これだけです。

于 2012-06-15T23:44:32.683 に答える
3

私は同じ問題を抱えていて、カスタムviewGroupを作成し、その中に線形レイアウトviewGroupを追加しましたが、線形レイアウトの内容を見ることができませんでした。カスタムViewGroupに次のコードを追加して問題を解決しました:-

@Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // TODO Auto-generated method stub
            final int count = getChildCount();
            for (int i = 0; i < count; i++) {
                childView = getChildAt(i);
                childView.measure(widthMeasureSpec, heightMeasureSpec);
            }
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        }
于 2012-09-26T06:37:11.653 に答える