2012-03-19 176 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); 
} 

並取得了理想的結果,而不是你發現了一個。 也許你應該檢查填充類型裏面的東西佈局。