Android--添加子视图(addView和setView)

来源:互联网 发布:php将文件夹压缩成zip 编辑:程序博客网 时间:2024/06/13 17:51

我们在添加视图文件的时候有两种方式,一种是通过在xml文件定义layout,另一种方式是在java代码中动态生成布局文件。

在xml中定义的layout要想转化为view,需要使用到LayoutInflater类。

1.构造xml文件

2.LayoutInflater

提到addview,首先要了解一下LayoutInflater类。这个类最主要的功能就是实现将xml表述的layout转化为View的功能。为了便于理解,我们可以将它与findViewById()作一比较,二者都是实例化某一对象,不同的是findViewById()是找xml布局文件下的具体widget控件实例化,而LayoutInflater找res/layout/下的xml布局文件来实例化的。

(1)创建

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);或

LayoutInflater inflater = LayoutInflater.from(Activity.this);或

LayoutInflater inflater = getLayoutInflater();

这三种方法本质是相同的。

(2)inflate()

用LayoutInflater.inflate() 将LayOut文件转化成VIew。

View view = inflater.inflate(R.layout.login, null);

3.添加视图文件

举个例子,假如定义了一个toast,则可以设置视图文件

toast.setView(view);

====

现在给出一个常用的土司烤面包的例子--让带图片和文本的面包居中显示,看代码:

其中主文件只放置了一个button,xml文件就不赘述。

[java] view plaincopyprint?
  1. package com.cn.query;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.ImageView;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.TextView;  
  12. import android.widget.Toast;  
  13.   
  14. import com.androidquery.AQuery;  
  15.   
  16. public class AQueryTest2 extends Activity {  
  17.     AQuery aq = new AQuery(this);  
  18.     private Button button;  
  19.   
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         // TODO Auto-generated method stub  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.test1);  
  24.         aq.id(R.id.button1).visible().clicked(this"click");  
  25.     }  
  26.   
  27.     public void click() {  
  28.         // 动态生成布局视图--适用于简单布局  
  29.         Toast toast = new Toast(AQueryTest2.this);  
  30.         toast.setDuration(3000);  
  31.         // 设置重心--让toast居中显示  
  32.         toast.setGravity(Gravity.CENTER, 00);  
  33.         LinearLayout ll = new LinearLayout(AQueryTest2.this);  
  34.         ImageView iv = new ImageView(AQueryTest2.this);  
  35.         iv.setImageResource(R.drawable.icon1);  
  36.         // 设置图片内边距,使textview显示在右侧,避免重叠  
  37.         iv.setPadding(00150);  
  38.         // 布局属于ViewGroup,可以调用添加视图方法  
  39.         ll.addView(iv);  
  40.         TextView textview = new TextView(AQueryTest2.this);  
  41.         textview.setText("我是创建消息的提示框");  
  42.         //  
  43.         ll.addView(textview);  
  44.         toast.setView(ll);  
  45.         toast.show();  
  46.     }  
  47.   
  48.     public void click2() {  
  49.         // 动态生成布局视图--适用于复杂UI布局  
  50.         Toast toast = new Toast(AQueryTest2.this);  
  51.         toast.setDuration(3000);  
  52.         // 设置重心  
  53.         toast.setGravity(Gravity.CENTER, 00);  
  54.         // 创建inflater  
  55.         LayoutInflater inflater = getLayoutInflater();  
  56.         // 通过inflate方法将layout转化为view  
  57.         View view = inflater.inflate(R.layout.toast, null);  
  58.         // 设置视图--Toast继承自Widget,不是容器,只能调用设置视图方法  
  59.         toast.setView(view);  
  60.         toast.show();  
  61.     }  
  62. }  
clcik()方法是动态生成的布局,就不多说了。注意ll.addView(iv)这里用的是addView,因为LinearLayout继承自ViewGroup,所以是个容器,容器添加视图则用addView().

click2()方法时将layout定义在xml文件,然后通过LayoutInflater类的实例化对象 inflater调用inflate方法将layout转化为view。注意toast.setView(),Toast是widget,不是容器,只能用setView()设置视图。

click2()方法中使用的布局文件:

toast.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center_vertical|center_horizontal"  
  6.     android:orientation="horizontal" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/imageview3"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:paddingBottom="0px"  
  13.         android:paddingLeft="0px"  
  14.         android:paddingRight="5px"  
  15.         android:paddingTop="0px"  
  16.         android:src="@drawable/icon1" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/textview3"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="消息提示" />  
  23.   
  24. </LinearLayout>  


除此之外上面还用到的Android Aquery轻量级插件。需要导入相应的包就可。

效果截图:





0 0
原创粉丝点击