今天,简单讲讲android里如何使用


addView动态的添加控件到界面上。

这个其实很简单,之前我使用时发生了一些问题,所以在网上查找了资料,最终解决了问题。这里记录一下。

一、说明

添加视图文件的时候有两种方式:1、通过在xml文件定义layout;2、java代码编写

这里只介绍代码添加视图文件。

 在项目开发中,我们经常需要进行动态添加组件,其中可添加的部分有两项:布局和组件

 其中:

            添加的布局主要有RelativeLayout型(相对布局)的和LinearLayout(线性布局)

            添加的组件主要有文本显示框,编辑框,按钮等组件。

首先我们创建一个新的项目,删除MainActivity.class中没有用的代码,仅留下protected void onCreate(Bundle savedInstanceState)函数

往布局文件中添加一个新的组件:

 1. addView方法简介

       在Android中,addView(ViewGroup view, index)在指定的index处添加一个view。addView是继承viewGroup的方法,

void android.view.ViewGroup.addView(View child);
void android.view.ViewGroup.addView(View child, LayoutParams params);
void android.view.ViewGroup.addView(View child,int index, LayoutParams params);

其中需要注意的是 index ,在linearlayout中使用addView的时候,如果linearlayout方向是vertical 垂直, index代表添加的child的view在linearlayout的行数,index是0,表示添加的child在linearlayout顶部,-1为底部,可以利用排版View的 addView 函数,将动态产生的View 组件加入到排版View 中。

2、示例:

      (1)首先我们往布局文件中添加一个组件,比如一个文本,两个按钮,此时我们需要在布局文件中添加一个布局项<LinearLayout>,定义其id为linearlay_1,用于在添加组件时识别,布局文件代码如下所示:

    <TextViewandroid: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类里边进行添加组件,代码如下所示:

   /**  

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

      *注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。

  */

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//绑定activity_main布局文件中的布局项,其中R.id.lenearlay_1为布局文件中设置的idLinearLayout 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 );}}

效果如下图所示:

android addView的使用-编程之家

图 1 动态添加组件-LinearLayout

(2) 动态添加布局:

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

*/这次只是在MainActivity中进行操作,不涉及布局文件(.xml),其代码如下所示:

public class MainActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);////创建一个相对布局relativeRelativeLayout 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);}}

效果如下所示:

android addView的使用-编程之家

图 2 动态添加布局-RelativeLayout

这里简单讲讲,如果是使用void android.view.ViewGroup.addView(View child,int index, LayoutParams params);  时,需要注意index的值需要从0开始,每次顺序增加1,否则会抛出异常。自己查看了addView的源码,发现addView里面每次添加控件时有一个包含控件的计数的变量,如果addView(ViewGroup view, index)的index与计数变量不同,就会抛出异常。

android addView的使用就讲完了。

就这么简单。