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中添加工具栏还是头一次.在这里记录一下,给自己留 ...
随机推荐
- angular 按需加载
angular.module('app',[]) .controller('ctrl',function ($http,$scope){ //ctrl控制器,名称作用的范围 html中ng-cont ...
- awk使用入门
1.基本用法 awk '{pattern + action}' {filenames} pattern 表示 AWK 在数据中查找的内容 action 是在找到匹配内容时所执行的一系列命令. patt ...
- 数据迁移sql
1.把数据库test中的表Table1中的数据插入到数据库test2中的表Table2:insert into test2.Table2(a,c,d) select a,c,5 from test.T ...
- [Javascript] Advanced Reduce: Additional Reducer Arguments
Sometimes we need to turn arrays into new values in ways that can't be done purely by passing an acc ...
- android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)
效果图 : 分别是第一页.第二页.第三页,随手截的图,不整齐,勿见怪.开始走了弯路,废了不少时间. 思路如下: 1.用ViewPager实现左右分页滑动 ...
- Oracle按用户进行统计信息更新
按用户进行统计信息更新 PL/sqldev工具使用system用户连接到oracle,打开命令窗口执行以下SQL,用户名请根据实际情况进行更改: begin dbms_stats.gather_sch ...
- C#第一节课
1,命名规范 A.如果声明一个变量,小写,如果有多个单词,后面首字母大写 如: string sString="aa"; int iNum=20; bool bMale=false ...
- web页面打印
在使用的两种方式打印: 第一种:js如下 function doPrint() { allhtml = window.document.body.innerHTML; starstr = " ...
- 使用__doPostBack函数来达到使用客户端的控件来调用服务器端的函数的--小结
类比LinkButton按钮 LinkButton前台生成代码: JS代码: //<![CDATA[ var theForm = document.forms['form1']; if (!th ...
- MySQL分支Percona,折腾中,先科普一下
官方网站:http://www.percona.com/ Percona 为 MySQL 数据库服务器进行了改进,在功能和性能上较 MySQL 有着很显著的提升.该版本提升了在高负载情况下的 Inno ...