本文主要讲述如何动态给UI界面添加布局和控件,在编程的时候很多时候需要动态显示一些内容,在动态添加View的时候,主要使用addView方法。

1. addView方法简介

      在Android 中,可以利用排版View的 addView 函数,将动态产生的View 物件加入到排版View 中。

例子如下:

界面代码:

 


Activity代码:

 

public class helloWorld extends Activity {    public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView( R.layout.main );       // 取得LinearLayout 物件      LinearLayout ll = (LinearLayout)findViewById(R.id.viewObj);       // 将TextView 加入到LinearLayout 中      TextView tv = new TextView(this);      tv.setText("Hello World");      ll. addView ( tv );       // 将Button 1 加入到LinearLayout 中      Button b1 = new Button(this);      b1.setText("取消");      ll. addView ( b1 );       // 将Button 2 加入到LinearLayout 中      Button b2 = new Button(this);      b2.setText("确定");      ll. addView ( b2 );       // 从LinearLayout 中移除Button 1      ll. removeView ( b1 );   }}


      上述代码的位置,是垂直顺序排列的因为界面代码Linerlayout的orientation设置的是vertical的,但是为了美观,需要设置添加的View的位置和样式。在添加View的时候分为两类来介绍,一种是布局(例如:Linearlayout等),一种是控件(例如:Button,TextView等等。)

2. 动态添加布局(包括样式和位置)

      下面的例子将介绍如何动态添加布局,基本内容和上面的代码一致,主要注重如何控制添加的布局的位置。在控制布局的位置的时候使用LayoutParam类来实现。

例子:

      界面代码和上面的界面代码类似,就不在重复介绍。

Activity类部分代码:

 

RelativeLayout rl = new RelativeLayout(this);//设置RelativeLayout布局的宽高RelativeLayout.LayoutParams relLayoutParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);this.addView(rl, relLayoutParams);


 

3. 动态添加控件

       动态添加控件和添加布局很相似,下述代码主要注重看控制控件的位置,下面的代码和第二项添加布局的补充,在新添加的布局里面再添加控件。

界面代码同样不在重复。

Activity类部分代码:

 

RelativeLayout rl = new RelativeLayout(this);//设置RelativeLayout布局的宽高RelativeLayout.LayoutParams relLayoutParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);TextView temp = new TextView(this);temp .setId(1);temp.setText(“图片”);rl.addView(temp);TextView tv = new TextView(this);tv.setText(“文字”);tv.setId(2);LayoutParams param1 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);param1.addRule(RelativeLayout.BELOW, 1);//此控件在id为1的控件的下边rl.addView(tv,param1);Button update = new Button(this);update.setText("Button");LayoutParams param2 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);param2.addRule(RelativeLayout.RIGHT_OF, 1);//此控件在id为1的控件的右边rl.addView(update,param2);this.addView(rl, relLayoutParams);


注意:控制位置和样式的时候,布局和控件使用的方法是一样的。

4. 位置参数介绍

参见网址:http://blog.sina.com.cn/s/blog_5da93c8f0101azq7.html

5. 参考网址:

(1)http://blog.csdn.net/frdde/article/details/7289047

(2)http://blog.csdn.net/xanxus46/article/details/7742446

(3)http://blog.sina.com.cn/s/blog_5da93c8f0101azq7.html

(4)http://blog.sina.com.cn/s/blog_4ac60f270100myjk.html

 

 

 

更多相关文章

  1. Android(安卓)UI框架 Android(安卓)UI控件类简介 android5大布局
  2. Android中添加自定义按键 ---- 非标准做法
  3. Android高手进阶教程(十七)之---Android中Intent传递对象的两种
  4. Android中UI设计的一些技巧!!!
  5. 写给Android开发者的Kotlin入门
  6. 8步教你打开Android之门 NDK入门教程
  7. 将服务器端字符读取至android的文本控件,换行符变成了黑方块的原
  8. Android上鲜为人知的UI控件介绍和使用
  9. 解决android一直在running,打不开文件,下载不了gradle包的方法

随机推荐

  1. Android(安卓)智能手机开发概述
  2. 高质量技术文章
  3. android三种动画详解
  4. android基本架构
  5. Android开发之——底层驱动开发(-)
  6. ui布局参数设置
  7. android:scaleType 属性
  8. Android(安卓)Fingerprint完全解析(三) :F
  9. Android基础知识之Manifest文件的组织结
  10. android imageView 图片显示方式属性