简体   繁体   English

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

[英]How to Inflate view from XML in Android?

I'm creating a tableLayout [given in XML] 我正在创建一个tableLayout [以XML提供]

adding table Row [created in XML and inflating in Java] 添加表Row [以XML创建并以Java夸大]

also adding 2 textview to the table Row [created in XML and inflating in JAVA] 还向表Row添加了2个textview [以XML创建并以JAVA夸大]

I'm able to get only the background and textcolors but not the layout properties like width, height and margin to get table view. 我只能获取背景和文本颜色,而无法获取诸如表格的宽度,高度和边距之类的布局属性。

  1. First declare your inflater. 首先声明您的充气机。

     LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService (Context.LAYOUT_INFLATER_SERVICE); 
  2. Identify and inflate the new view you seek to project on the current view. 标识并扩大要在当前视图上投影的新视图。

     View view = inflater.inflate(R.layout.new_layout,null); 
  3. You would want to add your new inflated view to your layout. 您可能希望将新的展开视图添加到布局中。

     main.addView(view); 

You can reference additional information here: http://developer.android.com/reference/android/view/LayoutInflater.html 您可以在此处引用其他信息: http : //developer.android.com/reference/android/view/LayoutInflater.html

Update May 2019 (Kotlin): This is how you would inflate a view from XML in Kotlin. 2019年5月更新(Kotlin):这是您如何在Kotlin中从XML扩展视图。 this is referring to an activity. 这是指一项活动。

val view = this.layoutInflater.inflate(R.layout.dialog_upgrade, null)
mainLayout.addView(view)
LayoutInflater li = LayoutInflater.from(getApplicationContext());
View cv = li.inflate(R.layout.your_layout, null);

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

follow the above to inflate view. 请按照上面的步骤充实视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM