Android 添加子视图(addView和setView)
我们在添加视图文件的时候有两种方式,一种是通过在xml文件定义layout,另一种方式是在java代码中动态生成布局文件。
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);
toast.setView(view);
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();
}
}
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 添加子视图(addView和setView)的更多相关文章
- iOS开发-在表单元中添加子视图
#import <UIKit/UIKit.h> @interface NameAndColorCellTableViewCell : UITableViewCell @property(c ...
- 关于cell中添加子视图 复用重叠问题的解决方法
问题本质: 因为你要添加的子视图并不是在自定义的cell中实现的,而是根据系统给的UITableViewCell这个类创建的实例,每次进图 cellForRow方法都会创建一个cell,每次都要创 ...
- UIView 添加子视图的常用方法
1. - (void)addSubview:(UIView *)view 这是最常用的方法有两个注意点 参数view可以是nil,运行不会报错,当然,父视图的subViews也不会增加. 此方法增加 ...
- 动态添加子视图 UIView 的正确方法
很多时候哥比较喜欢用代码添加视图,特别是要同时加很多UIView时,而且跟 xib 比起来代码更容易管理,在多人的项目中代码不容易 conflict. 但小牛哥最近发现很多新人都不太清楚正确的使用方法 ...
- 【iOS开发】动态添加子视图 UIView 的正确方法
很多时候哥比较喜欢用代码添加视图,特别是要同时加很多UIView时,而且跟 xib 比起来代码更容易管理,在多人的项目中代码不容易 conflict. 但小牛哥最近发现很多新人都不太清楚正确的使用方法 ...
- UIView回调方法(可以在添加子视图等,做一些额外操作)
didAddSubview didMoveToSuperview willMoveToSuperview didMoveToWindow willMoveToWindow willRemoveSubv ...
- 【转】 UIView如何管理它的子视图
原文:http://my.oschina.net/u/1984662/blog/293690 目录[-] Core Animation基础 改变视图的层 动画支持 视图坐标系统 边框.边界.和中心的关 ...
- HackSix 为ViewGroup的子视图添加悦目的动画效果
1.默认情况下他,添加到viewGrop的子视图是直接显示出来的.有一个比较简单的方法可以为这个过程增加动画效果. 2.知识点: 给子视图添加动画效果就用:LayoutAnimationCon ...
- 如何在BCGControlBar界面库的CBCGPFormView子视图里面添加工具栏
最近有一个项目需求,需要在子视图里面添加一个新工具栏用来处理当前视图对应模块的操作.之前在对话框模式下做过添加工具栏的实现,在CBCGPFormView中添加工具栏还是头一次.在这里记录一下,给自己留 ...
随机推荐
- requirejs2读书笔记
If you want to do require() calls in the HTML page, then it is best to not use data-main. data-main ...
- log4j级别输出
log4j 我们知道: log4j.logger.XX cover ==> log4j.rootLogger log4j.appender.XX.Threshold决定了最低接收级别 也就是说 ...
- Linux内核-内核线程
线程分类:内核线程.用户线程(指不需要内核支持而完全建立在用户空间的线程库,这种线程效率高,由于Linux内核没有轻量级进程(线程)的概念,因此不能独立的对用户线程进行调度,而是由一个线程运行库来组织 ...
- vmware workstation11虚拟机破解版(附安装教程) 32/64位
http://kuai.xunlei.com/d/ru4IAALVJQBesH9U93e?p=20395 vmware workstation 11注册机是一款可以免费生成vmware11.0版本序列 ...
- Unix系统解压tar包时出现@LongLink错误
Unix系统上使用tar命令解压tar包后,多了一个@LongLink的文件,并且原来的tar包解压后不完整.网上查了下,原因是AIX系统上tar命令自身的一个缺陷.解决办法:把该tar包上传到lin ...
- C#总结项目《影院售票系统》编写总结二
昨天发布了总结项目的第一篇,需求分析以及类的搭建,今天继续更新,动态绘制控件.票类型的切换以及数据在窗体中的展现. 先从简单的开始,票类型的切换. 分析: 1.当点击普通票时 学生折扣和赠送者是禁用的 ...
- 使程序能够引入.json文件, 为网站添加 MIME 映射
确认启用了 MIME 映射,或使用命令行工具 appcmd.exe 为网站添加 MIME 映射. 若要设置 MIME 类型,请在 IIS Express 安装目录中运行以下命令: appcmd set ...
- 9、第九节课jquery选择器jq2,20151007
1.表单选择器 2.not 里面不能加其他标签 $div p:not(not:disable) 错误的 $div p:not(:disable) 正确的 3.选择设置相应属性的标签项 $(&quo ...
- 5、第5节课CSS补充和html 标签讲解20150924
1. DIV 隐藏 A: 隐藏之后不占位置 display:none; B:隐藏之后占位置 visibility:hidden; 2.DIV 排序 z-index:2; 默认是1,如果想DIV在上 ...
- php的mq客户端获取队列方法改造
获取mq中消息然后处理失败重试机制: 下面的代码是php连接mq客户端的获取queue队列中的消息代码: public function createDurableSubscriber($queue, ...