2012-03-19 170 views
0

我有一个LinearLayout,我正在扩展,我想动态添加一个ViewFlipper和视图。谁能告诉我为什么这显示一个空白的屏幕?Android - 动态添加视图

protected void onLayout(boolean changed, int l, int t, int r, int b) 
{ 
    ViewFlipper vf = new ViewFlipper(context);  
    vf.layout(l, t, r, b); 
    this.addView(vf);      

    TextView tv = new TextView(context); 
    tv.setText("FOO"); 
    tv.setBackgroundColor(Color.WHITE); 

    vf.addView(tv, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

}

+0

尝试将相应的'LayoutParams'设置为您的ViewFlipper。 – 2012-03-19 20:39:35

+0

请提供活动,否则很难说。 – Jan 2012-03-19 20:44:55

+0

在'onLayout'中更改布局对我来说看起来不是个好主意。你确定你想要有不同的布局,取决于你的自定义LinearLayout的大小。目前,无论何时需要重新布置自身,可能会多次出现新的ViewFlippers。 – zapl 2012-03-19 21:15:22

回答

2

你重写的onMeasure()方法?我认为你需要这样做,否则视图大小不正确。有人请纠正我,如果我错了

4

我做了这个示例代码:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.main); 

    ViewFlipper vf = new ViewFlipper(this);  
    vf.layout(10, 100, 100, 150); 
    LinearLayout ll = new LinearLayout(this); 
    TextView tv = new TextView(this); 
    tv.setText("Prueba"); 
    ll.addView(tv); 
    ll.addView(vf); 



    TextView tv2 = new TextView(this); 
    tv2.setText("FOO"); 
    tv2.setBackgroundColor(Color.WHITE); 

    vf.addView(tv2, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 
    setContentView(ll); 
} 

并取得了理想的结果,而不是你发现了一个。 也许你应该检查填充类型里面的东西布局。