如何在Android中从XML扩展视图?

时间:2012-04-12 12:01:55

标签: android user-interface view android-tablelayout android-inflate

我正在创建一个tableLayout [以XML格式给出]

添加表格[用XML创建并在Java中膨胀]

还在表格[在XML中创建并在JAVA中充气]

中添加2个textview

我只能获得背景和文本颜色,但不能获得宽度,高度和边距等布局属性来获取表格视图。

3 个答案:

答案 0 :(得分:45)

  1. 首先声明你的inflater。

    LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);
    
  2. 识别并扩充您希望在当前视图上投影的新视图。

    View view = inflater.inflate(R.layout.new_layout,null);
    
  3. 您可能希望将新的充气视图添加到布局中。

    main.addView(view);
    
  4. 您可以在此处参考其他信息:http://developer.android.com/reference/android/view/LayoutInflater.html

    2019年5月更新(Kotlin): 这就是你如何在Kotlin中从XML中扩展视图。这指的是一项活动。

    val view = this.layoutInflater.inflate(R.layout.dialog_upgrade, null)
    mainLayout.addView(view)
    

答案 1 :(得分:11)

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

mainlayout.addView(cv);

答案 2 :(得分:1)

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

按照上述说明来扩充视图。