是静态和动态的布局可能? [英] Is both Static and Dynamic Layout Possible?

查看:116
本文介绍了是静态和动态的布局可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单一的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(但);

LayoutInflater

或者,如果你想将它添加到当前文件中的布局你可以使用 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 and Layouts as you are doing with setText(). 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 the Buttons to it with addView(). Something like

Layoutinflater 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);

LayoutInflater

Or if you want to add it to a layout in the current file you can just use findViewById() and use addView() on that to add your Buttons to.

这篇关于是静态和动态的布局可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆