分享

Android 利用addView 动态给Activity添加View组件

 gearss 2016-05-30
Android 利用addView 动态给Activity添加View组件
2014-09-24      0 个评论    来源:璐璐的专栏  
收藏    我要投稿

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

1. addView方法简介

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

例子如下:

界面代码:

 

1
<linearlayout android:id="@+id/viewObj" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_x="1px" android:layout_y="1px" android:orientation="vertical"></linearlayout>

 

Activity代码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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类部分代码:

 

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

 

 

3. 动态添加控件

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

界面代码同样不在重复。

Activity类部分代码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);

 

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

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多