我们在添加视图文件的时候有两种方式,一种是通过在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);
现在给出一个常用的toast的例子--让带图片和文本的toast居中显示,看代码:
其中主文件只放置了一个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();
// 设置重心--让toast居中显示
toast.setGravity(Gravity.CENTER, , );
LinearLayout ll = new LinearLayout(AQueryTest2.this);
ImageView iv = new ImageView(AQueryTest2.this);
iv.setImageResource(R.drawable.icon1);
// 设置图片内边距,使textview显示在右侧,避免重叠
iv.setPadding(, , , );
// 布局属于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();
// 设置重心
toast.setGravity(Gravity.CENTER, , );
// 创建inflater
LayoutInflater inflater = getLayoutInflater();
// 通过inflate方法将layout转化为view
View view = inflater.inflate(R.layout.toast, null);
// 设置视图--Toast继承自Widget,不是容器,只能调用设置视图方法
toast.setView(view);
toast.show();
}
}
click()方法是动态生成的布局,就不多说了。注意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 添加子视图(addView和setView)的更多相关文章

  1. iOS开发-在表单元中添加子视图

    #import <UIKit/UIKit.h> @interface NameAndColorCellTableViewCell : UITableViewCell @property(c ...

  2. 关于cell中添加子视图 复用重叠问题的解决方法

    问题本质:   因为你要添加的子视图并不是在自定义的cell中实现的,而是根据系统给的UITableViewCell这个类创建的实例,每次进图 cellForRow方法都会创建一个cell,每次都要创 ...

  3. UIView 添加子视图的常用方法

    1.  - (void)addSubview:(UIView *)view 这是最常用的方法有两个注意点 参数view可以是nil,运行不会报错,当然,父视图的subViews也不会增加. 此方法增加 ...

  4. 动态添加子视图 UIView 的正确方法

    很多时候哥比较喜欢用代码添加视图,特别是要同时加很多UIView时,而且跟 xib 比起来代码更容易管理,在多人的项目中代码不容易 conflict. 但小牛哥最近发现很多新人都不太清楚正确的使用方法 ...

  5. 【iOS开发】动态添加子视图 UIView 的正确方法

    很多时候哥比较喜欢用代码添加视图,特别是要同时加很多UIView时,而且跟 xib 比起来代码更容易管理,在多人的项目中代码不容易 conflict. 但小牛哥最近发现很多新人都不太清楚正确的使用方法 ...

  6. UIView回调方法(可以在添加子视图等,做一些额外操作)

    didAddSubview didMoveToSuperview willMoveToSuperview didMoveToWindow willMoveToWindow willRemoveSubv ...

  7. 【转】 UIView如何管理它的子视图

    原文:http://my.oschina.net/u/1984662/blog/293690 目录[-] Core Animation基础 改变视图的层 动画支持 视图坐标系统 边框.边界.和中心的关 ...

  8. HackSix 为ViewGroup的子视图添加悦目的动画效果

    1.默认情况下他,添加到viewGrop的子视图是直接显示出来的.有一个比较简单的方法可以为这个过程增加动画效果. 2.知识点:     给子视图添加动画效果就用:LayoutAnimationCon ...

  9. 如何在BCGControlBar界面库的CBCGPFormView子视图里面添加工具栏

    最近有一个项目需求,需要在子视图里面添加一个新工具栏用来处理当前视图对应模块的操作.之前在对话框模式下做过添加工具栏的实现,在CBCGPFormView中添加工具栏还是头一次.在这里记录一下,给自己留 ...

随机推荐

  1. ubuntu下mysql的常用命令

    首先安装mysql:sudo?apt-get?install?mysql-server?mysql-client? 1.终端启动MySQL:/etc/init.d/mysql start:(stop ...

  2. Robotium--scroll操作系列

    上下滚动 scrollDown public boolean scrollDown() Scrolls down the screen. Returns: true if more scrolling ...

  3. php中运用GD库实现简单验证码

    昨天学习了运用php的GD库进行验证码的实现. 首先可以用phpinfo()函数看一下GD库有没有安装,我用的wampserver是自动给安装的. 主要的步骤是: 1.生成验证码图片 2.随机生成字符 ...

  4. 常用PC服务器LSI阵列卡配置

    通常,我们使用的DELL/HP/IBM三家的机架式PC级服务器阵列卡是从LSI的卡OEM出来的,DELL和IBM两家的阵列卡原生程度较高,没有做太多封装,可以用原厂提供的阵列卡管理工具进行监控:而HP ...

  5. Sorting File Contents and Output with sort

     Sorting File Contents and Output with sort   Another very useful command to use on text file is  so ...

  6. int? 类型数据

    在数据库操作中,会遇到在int的单元格恰好为NULL值的情况,这个时候我们可以直接判断是否为null然后进行赋值,有人就想那我刚好用一下:?表达式不就好了: ) ? ); 这时候编译器会报错,原因就是 ...

  7. 渲染器 Shader BitmapShader

    渲染模式: tileX tileY:The tiling mode for x/y to draw the bitmap in.   在位图上 X/Y 方向 瓦工/花砖/瓷砖 模式 CLAMP  :如 ...

  8. ASP.NET-FineUI开发实践-8(二)

    把上回的做一些改进 1.点击grid2的行改变TriggerBox1的值 var v = $(item).find('.x-grid-cell-Name div.x-grid-cell-inner') ...

  9. 在vim中设置 '打印时间'的快捷键.

    在 ~/.vimrc (没有该文件可以手动创建)中输入 map <F4> <Esc>:r !date<CR> 实现在 '一般模式'状态点击 F4时,自动在vim中打 ...

  10. 解决Cacti监控图像断断续续问题

    最近cacti的图像全都是断断续续.新加的设备,图像也是这样,查看cacti 的log发现大量下面类似的错误信息:04/12/2011 03:54:37 PM - SPINE: Poller[0] H ...