当前位置:首页 » 安卓系统 » androidview动态添加

androidview动态添加

发布时间: 2024-10-20 03:00:39

❶ 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中如何使几个view动起来随机交换位置呢

这是做的一个部分功能,因为你这个要求起来还是挺难控制算法的。 view的点击事件可以使用OnTouch事件做。 实现功能: 绘制三个图片,移动位置,红蓝移动一次,要全部的话,算法要很精妙,时间控制也很难,我这个就不说了,我的算法很菜。希望能给你点启发。

开始图:

❸ android viewpager怎么动态加载view而且view的个数是不一定的。

在自定义适配器里写一个方法add方法,加载View的时候,增加到适配器中
适配器需要重写
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
增加数据到适配器后需要调用adapter.notifyDataSetChanged();

❹ android开发 如何在相对布局中动态添加控件

首先setMargin方法不是RelativeLayout的方法,而是RelativeLayout.LayoutParams的方法。
你应该这麼用:
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(-1, -1));
TextView mView = new TextView(this);
mView.setId(2);
mView.setText("this is a test text!");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
-2, -2);
// layoutParams.setMargins(100, 100, 100, 100);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);
layout.addView(mView, layoutParams);
上例是将一个TextView添加到RelativeLayout的底部。你可以把注释行取消掉,把下一行注释,再看下效果。

热点内容
我的世界纯生存服务器地址117 发布:2024-10-20 05:25:14 浏览:414
编译器语法分析器 发布:2024-10-20 05:08:31 浏览:799
javajdbc数据库连接 发布:2024-10-20 04:50:02 浏览:10
android获取子控件 发布:2024-10-20 04:11:13 浏览:242
java接口的作用和意义 发布:2024-10-20 04:07:46 浏览:172
安卓如何用耳机内录 发布:2024-10-20 04:01:31 浏览:243
asp无组件上传进度 发布:2024-10-20 03:59:59 浏览:110
服务器依据端口搭建多个网站 发布:2024-10-20 03:56:08 浏览:749
o圈压缩量 发布:2024-10-20 03:37:00 浏览:857
光遇为什么显示登入服务器错误 发布:2024-10-20 03:18:02 浏览:970