11

我創建一個tableLayout [在XML給出]如何在Android中充實XML視圖?

添加錶行[在XML創建並在Java中充氣]

還加入2的TextView錶行[在XML創建並在JAVA充氣]

我能夠得到的只有背景和textcolors但不喜歡的寬度,高度和利潤率佈局屬性來獲取表視圖。

+2

請發表您的XML文件,並在那裏你誇大它 – Triode 2012-04-12 12:04:13

+0

正如你所提到的,您有每個錶行2個textviews了java的代碼,那麼我會建議你實現一個ListView與自定義適配器。 – 2012-04-12 12:18:03

+0

<?XML版本= 「1.0」 編碼= 「UTF-8」?> 2012-04-12 12:42:47

回答

10
LayoutInflater li = LayoutInflater.from(getApplicationContext()); 
View cv = li.inflate(R.layout.your_layout, null); 

mainlayout.addView(cv); 
1
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService  (Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.your_layout,null); 
mainlayout.addView(view; 

按照上面的膨脹視圖。

+0

怎麼弄mainLayout?我們設定的setContentView(佈局)中的onCreate() – 2012-04-12 12:44:10

+0

mainlayout將是你tableLayout比如在你的情況 – 2012-04-12 12:46:23

+0

是的,我得到了你的觀點,也做了同樣的方式。我可以得到View組件[TextView的在屏幕上,但只適用BG和文本顏色,而不是佈局屬性正如我在上面的評論 – 2012-04-12 12:50:04

38
  1. 首先聲明您的充氣器。

    LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService 
        (Context.LAYOUT_INFLATER_SERVICE); 
    
  2. 確定並膨脹您試圖在當前視圖上投影的新視圖。

    View view = inflater.inflate(R.layout.new_layout,null); 
    
  3. 你想添加新的充氣視圖到你的佈局。

    main.addView(view); 
    

你可以在這裏引用的其他信息:http://developer.android.com/reference/android/view/LayoutInflater.html

相關問題