无法将自动视图添加到自定义视图组

时间:2012-06-15 17:46:14

标签: android xml view inflate viewgroup

我一直在尝试将视图从xml扩展到相应的视图对象,然后将这些视图对象添加到编程的视图组中。但是,每当我调用myviewgroup.addView(子视图)时,子视图似乎都会丢失xml代码应该给出的所有按钮和内容。但是,子视图确实具有的一个方面是我编写的背景颜色,以确保为子视图提供正确的尺寸。子视图具有正确的尺寸,但其组件均不可见

     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代码和背景颜色中显示出来。这个视图组的要点是,我可以在多个视图中的任何一个视图之间切换,而不管它们作为子视图的顺序。

以下是viewchanger的代码。我希望它有7个子视图,当我调用一个方法时 setView(int v)我希望所有其他视图都消失,我希望只有一个视图可见。

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>

3 个答案:

答案 0 :(得分:6)

在精神上经历了我能想到的一切之后,我创建了一个与此相同的虚拟项目来检查几种可能性。出于某种原因,从FrameLayout而不是ViewGroup扩展ViewChanger使得这项工作成为可能 - 很可能是因为ViewGroup缺少渲染它的子视图所需的东西(我试图重写onDraw,但它仍然无法工作)。由于您一次只显示一个视图 - 通常使用FrameLayout - 我建议只进行此更改;它比在ViewGroup中隔离和修复丢失的绘图功能要容易得多。

public class ViewChanger extends FrameLayout {

应该是您需要进行的唯一更改才能使原始设计(从构造函数中添加视图)正常工作。

答案 1 :(得分: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);

        }

答案 2 :(得分:1)

检查 FrameLayout语句。希望它有效。

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);
FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView().findViewById(android.R.id.content);
frameLayout.addView(viewchanger);
相关问题