如何在 Android 中从 XML 膨胀视图? [英] How to Inflate view from XML in Android?
问题描述
我正在创建一个 tableLayout [以 XML 格式给出]
I'm creating a tableLayout [given in XML]
添加表格行[用XML创建并用Java膨胀]
adding table Row [created in XML and inflating in Java]
还将 2 个 textview 添加到表格 Row [用 XML 创建并在 JAVA 中充气]
also adding 2 textview to the table Row [created in XML and inflating in 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.
推荐答案
首先声明你的充气机.
First declare your inflater.
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
确定并扩展您要在当前视图上投影的新视图.
Identify and inflate the new view you seek to project on the current view.
View view = inflater.inflate(R.layout.new_layout,null);
您可能希望将新的膨胀视图添加到您的布局中.
You would want to add your new inflated view to your layout.
main.addView(view);
您可以在此处参考其他信息:http://developer.android.com/reference/android/view/LayoutInflater.html
You can reference additional information here: http://developer.android.com/reference/android/view/LayoutInflater.html
2019 年 5 月更新 (Kotlin):这就是在 Kotlin 中从 XML 扩展视图的方式.这是指一项活动.
Update May 2019 (Kotlin): This is how you would inflate a view from XML in Kotlin. this is referring to an activity.
val view = this.layoutInflater.inflate(R.layout.dialog_upgrade, null)
mainLayout.addView(view)
这篇关于如何在 Android 中从 XML 膨胀视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!