Android初学习 - 在Java code(布局容器)中动态添加控件
来源:互联网 发布:java 简单员工管理系统 编辑:程序博客网 时间:2024/06/03 21:15
以动态添加Button控件为例,添加其他控件同样道理。
1、addView
添加控件到布局容器
2、removeView
在布局容器中删掉已有的控件
代码如下:
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 生成一个LinearLayout,作为布局容器来动态添加3个Buttonfinal LinearLayout layout = new LinearLayout(this);layout.setOrientation(LinearLayout.VERTICAL);setContentView(layout);// 生成3个Buttonfinal Button btn1 = new Button(this);btn1.setText("1");btn1.setText("Button1");final Button btn2 = new Button(this);btn2.setText("2");btn2.setText("Button2");final Button btn3 = new Button(this);btn3.setText("3");btn3.setText("Button3");// 动态把三个Button添加到layout.addView(btn1);layout.addView(btn2);layout.addView(btn3);// 点击按钮时,先把原来在布局容器layout上的删掉,再添加上局容器layout,这样本次添加的控件就会排序到最后,以理解动态添加控件的思路btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {layout.removeView(btn1);layout.addView(btn1);}});// 同btn1一样道理btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {layout.removeView(btn2);layout.addView(btn2);}});// 同btn1一样道理btn3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {layout.removeView(btn3);layout.addView(btn3);}});setContentView(layout);}}
0 0
- Android初学习 - 在Java code(布局容器)中动态添加控件
- android 在布局中动态添加控件
- Android初学习 - 在布局中动态添加view的两种方法
- Android中如何在“自定义布局”中“动态”添加控件
- Android 在已有布局中动态添加控件
- Android 在程序中动态添加 View 布局或控件
- (Android开发)在已有布局中动态添加控件
- Android——在已有布局中动态添加控件
- Android 中动态的向布局中添加控件
- 在已有布局中动态添加控件
- Android在程序中动态生成控件,动态布局
- android在java代码中动态添加组件及相关布局方法(LayoutParams)
- 在Android代码中给布局添加控件
- Android中在activity用代码动态添加布局
- Android 动态添加布局(layout)和控件(Widget)
- Android根据布局文件动态添加控件
- wpf中利用布局动态添加控件
- android中在Layout布局文件中添加控件ID,在Activity中findViewById找不到
- SQLserver 用户进程堵塞解决方案;
- linux lsof详解
- Linux 编译器之 GCC
- WebDriver连接超时异常
- ViewPager+Fragment 滑动菜单效果 实现步骤
- Android初学习 - 在Java code(布局容器)中动态添加控件
- Json读写本地文件实现代码
- 用户密码存储方式
- SQL批量更新
- jstl标签forEach详解
- Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)
- arm开发板连接终端( xshell )
- Spark RDD API详解(一) Map和Reduce
- js加验证,正则表达式