是静态和动态的布局可能? [英] Is both Static and Dynamic Layout Possible?
问题描述
我有一个单一的TextView的XML布局
现在我想添加50个按钮,我想在我的Java文件动态地添加!
是否有可能将属性添加到通过Java code的XML文件?
或者可以活动在某一时刻2的布局?
有关如
公共类选项扩展活动
{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.options);
按钮,但=新按钮(本);
but.setText(日wassup);
//我如何添加此按钮的布局?
}}
是否有可能将属性添加到通过Java code的XML文件?
块引用>没有,但你可以到
查看添加属性
和布局
为您与做的setText()
。资源
文件本身不能改变编译后。
,也可以活动有2个布局在同一时间??
块引用>简单的答案是否定的,但你可以
膨胀
另一个布局,并把它添加到当前布局。你可以做添加
按钮有什么例子
充气根
布局
并添加按钮
将其与addView()
。类似Layoutinflater吹气=(LayoutInflater)getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LL的LinearLayout =(的LinearLayout)inflater.inflate(R.layout.layout_file);
按钮,但=新按钮(本);
but.setText(日wassup);
//我如何添加此按钮的布局?
ll.addView(但);或者,如果你想将它添加到当前文件中的
布局
你可以使用findViewById()
并使用addView()
上你的按钮添加
到I have an XML layout having a single TextView
Now I want to add 50 buttons which I want to add dynamically in my java file !.
Is it possible to add attributes to an XML file via java code ?? Or can an activity have 2 layouts at a time ??
for eg,
public class Options extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.options); Button but=new Button(this); but.setText("Wassup"); // How do I add this button to the layout ? } }
解决方案Is it possible to add attributes to an XML file via java code ??
No, but you can add properties to
Views
andLayouts
as you are doing withsetText()
.resource
files themselves cannot be changed after compiled.Or can an activity have 2 layouts at a time ??
The simple answer is no but you can
inflate
another layout and add it to the current layout.Example of what you can do to add a
Button
Inflate your root
layout
and add theButtons
to it withaddView()
. Something likeLayoutinflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE); LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.layout_file); Button but=new Button(this); but.setText("Wassup"); // How do I add this button to the layout ? ll.addView(but);
Or if you want to add it to a
layout
in the current file you can just usefindViewById()
and useaddView()
on that to add yourButtons
to.这篇关于是静态和动态的布局可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!