导航:首页 > 操作系统 > androidinflatexml

androidinflatexml

发布时间:2025-03-12 11:05:56

android 如何动态布局自定义view,不用XML.

可以直接new View来得到View对象来实现代码布局。以下为示例代码:
1.绝对布局
AbsoluteLayout abslayout=new AbsoluteLayout (this);
setContentView(abslayout);
Button btn1 = new Button(this);
btn1.setText(”this is a button”);
btn1.setId(1);
AbsoluteLayout.LayoutParams lp1 =
new AbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1, lp1);

2.相对布局
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
AbsoluteLayout abslayout=new AbsoluteLayout (this);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout.addView(abslayout ,lp1);

3.线性布局
LinearLayout ll = new LinearLayout(this);
EditText et = new EditText();
ll.addView(et);
//动态添加布局的方法1. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,ll); //这样 main2 作为 main1的子布局 加到了 main1的 根节点下
//动态添加布局的方法2 addView. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,null); ll.addView(ll2);

Ⅱ android 怎么把俩个xml文件整合到一起

private ViewGroup group;
private ViewGroup main;
main = (ViewGroup) inflater.inflate(R.layout.main, null);//这里的R。latout.main就是当前布局
group = (ViewGroup) main.findViewById(R.id.viewGroup);
group.addView(imageViews[i]);//这样就把一个imageview(你可添加一个任意复杂的View,这个View实例化的时候指向你所谓的其他布局文件)加入当前布局中的某个ViewGroup里了,你布局时可定义一个LinearLayout id 设为(R.id.viewGroup),oratation设为垂直,放到下面就是了。

阅读全文

与androidinflatexml相关的资料

热点内容
程序员放弃后会怎样 浏览:162
河北模具编程 浏览:179
adb查找命令 浏览:309
安卓手机视频文件夹怎么打开 浏览:303
平板加密手机后怎么关闭 浏览:557
流媒体服务器应该注意什么 浏览:529
d8命令编译 浏览:943
压缩包解压需要多少空间 浏览:139
如何查找app属性 浏览:381
android人脸识别技术 浏览:305
pc104编程 浏览:329
二维码反编译破解推广 浏览:674
修改服务器的mac地址 浏览:522
好玩的编程软件 浏览:892
编程语言创始人有钱吗 浏览:797
短视频app怎么获客 浏览:8
查看云服务器的应用 浏览:428
javadump工具 浏览:558
程序员16g 浏览:421
程序员没有办法成为top怎么办 浏览:197