导航:首页 > 操作系统 > 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相关的资料

热点内容
如何把一个安卓应用改为中文版 浏览:448
带求补器的源码 浏览:722
程序员掉入能力陷阱 浏览:671
中百超市多点app如何充值 浏览:331
仙剑奇侠传4codex未加密 浏览:194
如何搭建服务器ss端口 浏览:62
国外代理服务器地址服务器端口密码 浏览:754
phpthrownew 浏览:681
java从入门到精通pdf百度云 浏览:55
linuxhdf5安装 浏览:469
java继承编程练习题 浏览:652
云记app怎么写字视频 浏览:598
如何进入我的世界脏小豆的服务器 浏览:42
哪里找到转转app 浏览:580
苹果二次app怎么注册 浏览:587
国际服体验服服务器为什么爆满 浏览:114
微信商城模板源码 浏览:436
网页的源码怎么读取 浏览:402
ubuntu命令打开软件 浏览:761
如何实现对讲频道加密 浏览:351