在项目开发中,我们经常需要进行动态添加组件,其中可添加的部分有两项:布局和组件 其中,添加的布局主要有RelativeLayout型(相对布局)的和LinearL
在项目开发中,我们经常需要进行动态添加组件,其中可添加的部分有两项:布局和组件
其中,添加的布局主要有RelativeLayout型(相对布局)的和LinearLayout(线性布局)
添加的组件主要有文本显示框,编辑框,按钮等组件。
下面,就让我们来进行实现:
首先我们创建一个新的项目,删除MainActivity.class中没有的代码,仅留下protected void onCreate(Bundle savedInstanceState)函数往布局文件中添加一个新的组件:
1. addView方法简介
在Android中,addView(ViewGroup view, index)在指定的index处添加一个view。可以利用排版View的 addView 函数,将动态产生的View 物件加入到排版View 中。
2、示例:
(1)首先我们往布局文件中添加一个组件,比如一个文本,两个按钮,此时我们需要在布局文件中添加一个布局项<LinearLayout>,定义其id为linearlay_1,用于在添加组件时识别,布局文件代码如下所示:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态添加组件示例"
android:id="@+id/textview"/>
<LinearLayout
android:layout_below="@+id/textview"
android:id="@+id/linearlay_1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
然后我们在Activity类里边进行添加组件,代码如下所示:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定activity_main布局文件中的布局项,其中R.id.lenearlay_1为布局文件中设置的id
LinearLayout linear=(LinearLayout) findViewById(R.id.linearlay_1);
//添加文本,this代表当前项目
TextView tv=new TextView(this);
tv.setText("示例文本框");
tv.setId(1);//设置ID,可有可无,也可以在R文件中添加字符串,然后在这里使用引用的方式使用
linear.addView(tv);
// 将Button 加入到LinearLayout 中
Button b1 = new Button(this);
b1.setText("取消");
linear. addView ( b1 );
// 将Button 2 加入到LinearLayout 中
Button b2 = new Button(this);
b2.setText("确定");
linear. addView ( b2 );
// 从LinearLayout 中移除Button 1
// linear. removeView ( b1 );
}
}
效果如下图所示:
图 1 动态添加组件-LinearLayout
(2) 动态添加布局:
* 下面的例子将介绍如何动态添加布局,基本内容和上面的代码一致,主要注重如何控制添加的布局的位置
* 在控制布局的位置的时候使用LayoutParam类来实现。
* 注意:控制位置和样式的时候,布局和控件使用的方法是一样的。
*/这次只是在MainActivity中进行操作,不涉及布局文件(.xml),其代码如下所示:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//////////////////////////////////////
//创建一个相对布局relative
RelativeLayout relative = new RelativeLayout(this);
relative.setBackgroundColor(Color.YELLOW);
// 将Button1 加入到RelativeLayout 中
Button btn_r1 = new Button(this);
btn_r1.setText("取消");//设置显示的字符
btn_r1.setId(24);
relative.addView(btn_r1);
// 将Button2 加入到RelativeLayout 中
Button btn_r2 = new Button(this);
btn_r2.setText("确定");//设置显示的字符
btn_r2.setId(25);
relative.addView(btn_r2);
// 设置RelativeLayout布局的宽高
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
btn_r1.setLayoutParams(lp); ////设置按钮的布局属性
lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, btn_r1.getId());
btn_r2.setLayoutParams(lp); ////设置按钮的布局属性
setContentView(relative);
}
}
效果如下所示:
图 2 动态添加布局-RelativeLayout
学会了上面的介绍,你就可以很轻松的布局界面,无论是按钮还是其他组件,对于布局,你也可以很方便的进行布局使用,以上就是在安卓中如何动态添加组件的方法。
您可能感兴趣的文章:android ListView内数据的动态添加与删除实例代码Android动态添加menu菜单的简单方法在Android中动态添加Panel框架的实现代码Android 动态添加Fragment的实例代码Android用RecyclerView实现动态添加本地图片Android动态添加view的方法示例Android编程实现长按Button按钮连续响应功能示例Android实现圆角Button按钮Android开发中button按钮的使用及动态添加组件方法示例
--结束END--
本文标题: Android使用addView动态添加组件的方法
本文链接: https://www.lsjlt.com/news/23959.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0