当前位置:操作系统 > 安卓/Android >>

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

我们在添加视图文件的时候有两种方式,一种是通过在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文件就不赘述。
package com.cn.query;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.androidquery.AQuery;
 
public class AQueryTest2 extends Activity {
AQuery aq = new AQuery(this);
private Button button;
 
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
aq.id(R.id.button1).visible().clicked(this, "click");
}
 
public void click() {
// 动态生成布局视图--适用于简单布局
Toast toast = new Toast(AQueryTest2.this);
toast.setDuration(3000);
// 设置重心--让toast居中显示
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout ll = new LinearLayout(AQueryTest2.this);
ImageView iv = new ImageView(AQueryTest2.this);
iv.setImageResource(R.drawable.icon1);
// 设置图片内边距,使textview显示在右侧,避免重叠
iv.setPadding(0, 0, 15, 0);
// 布局属于ViewGroup,可以调用添加视图方法
ll.addView(iv);
TextView textview = new TextView(AQueryTest2.this);
textview.setText("我是创建消息的提示框");
//
ll.addView(textview);
toast.setView(ll);
toast.show();
}
 
public void click2() {
// 动态生成布局视图--适用于复杂UI布局
Toast toast = new Toast(AQueryTest2.this);
toast.setDuration(3000);
// 设置重心
toast.setGravity(Gravity.CENTER, 0, 0);
// 创建inflater
LayoutInflater inflater = getLayoutInflater();
// 通过inflate方法将layout转化为view
View view = inflater.inflate(R.layout.toast, null);
// 设置视图--Toast继承自Widget,不是容器,只能调用设置视图方法
toast.setView(view);
toast.show();
}
}
clcik()方法是动态生成的布局,就不多说了。注意ll.addView(iv)这里用的是addView,因为LinearLayout继承自ViewGroup,所以是个容器,容器添加视图则用addView().
click2()方法时将layout定义在xml文件,然后通过LayoutInflater类的实例化对象 inflater调用inflate方法将layout转化为view。注意toast.setView(),Toast是widget,不是容器,只能用setView()设置视图。
click2()方法中使用的布局文件:
toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="horizontal" >
 
    <ImageView
        android:id="@+id/imageview3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="0px"
        android:paddingLeft="0px"
        android:paddingRight="5px"
        android:paddingTop="0px"
        android:src="@drawable/icon1" />
 
    <TextView
        android:id="@+id/textview3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="消息提示" />
 
</LinearLayout>
 
 
除此之外上面还用到的Android Aquery轻量级插件。需要导入相应的包就可。
效果截图:
 
 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,