LayoutInflater的动态增加控件
在实际开发中LayoutInflater这个类是非常有用的,它的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件。
而findViewById()是查找的具体 widget控件(如Button,TextView等)。
1.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vertical_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
2.dynamic_add.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="25dip"
android:layout_height="25dip"
android:background="#ff0000"
android:text="dynamic_add" />
3.activity
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup parent = (ViewGroup) findViewById(R.id.vertical_container);
// result: layout_height=wrap_content layout_width=match_parent
//inflate(int resource, ViewGroup root)
//inflate 返回root view,如果提供root参数则返回root,否则返回resource的root
view = LayoutInflater.from(getBaseContext())
.inflate(R.layout.dynamic_add,null);
parent.addView(view);
// result: layout_height=100 layout_width=100
view = LayoutInflater.from(getBaseContext())
.inflate(R.layout.dynamic_add,null);
parent.addView(view, 100, 100);
// result: layout_height=25dip layout_width=25dip
view = LayoutInflater.from(getBaseContext())
.inflate(R.layout.dynamic_add,parent, false);
parent.addView(view);
// result: layout_height=25dip layout_width=25dip
// parent.addView(view) not necessary as this is already done by attachRoot=true
view = LayoutInflater.from(getBaseContext())
.inflate(R.layout.dynamic_add,parent, true);
}
View view = View.inflate(this, R.layout.dialog_layout, null);
TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
LayoutInflater的动态增加控件的更多相关文章
- android动态增加控件时控制样式的方法
在学习android的动画时,发现所谓的tween动画只是改变绘制效果并不改变原控件的位置时是颇为失望的,虽然3.0之后已经有了property animation,但是由于要兼容老版本的androi ...
- DOM动态增加控件
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...
- 在Extjs中动态增加控件
Ext.onReady(function () { Ext.QuickTips.init(); Ext.form.Field.prototype.msgTarget = 'side'; var aut ...
- Jquery 动态追加控件并获取值
先展示通过动态添加控件的结果: 实现步骤: 1.引用js文件 <script src="Script/jquery-1.5.1.min.js" type="text ...
- JQuery动态添加控件并取值
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- winform导入导出excel,后台动态添加控件
思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(s ...
- Android 在布局容器中动态添加控件
这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...
- VC中动态添加控件
VC中动态添加控件 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态控件: 为了对照,我们先来看一下静态控件的创建. 放置静态控件时必须先建立一个 ...
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
博客分类: jquery-easyui jQueryAjax框架HTML 现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...
随机推荐
- RP2836 板卡信息标识
RP2836 板卡信息标识 可以标识16种扩展应用 MCI_DA4 PD5 R120上拉 R121下拉 MCI_DA5 PD6 R125上拉 R124下拉 MCI_DA6 PD7 R122 ...
- am335x 内核频率 ddr3频率 电压调整
由Makefile可知,SPL的入口在u-boot-2011.09-psp04.06.00.08\arch\arm\cpu\armv7\start.S中 SPL的功能无非是设置MPU的Clock.PL ...
- div允许用户输入
主要是用到contenteditable属性,就可以用div让用户输入了 <div id="guo" style="width:500px; height:200p ...
- KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...
- dm8127之核间通信syslink
Last updated: June 23, 2010 Contents [hide] 1 About SysLink 1.1 SysLink Architecture 1.2 SysLink Usa ...
- 【NOIP模拟题】Permutation(dp+高精度)
首先我们可以这样想: 设状态f[i, j]表示1-i序列有j个'<'的方案数 那么考虑转移 因为i比i-1大,所以可以考虑从i-1来转移.首先i是要插入1-i-1这个序列的,所以我们可以思考插入 ...
- Nucleus PLUS的启动、执行线程和中断处理
nucleus系统是实时嵌入式操作系统,具有实时.任务抢先.多任务内核,当中95%的代码由C语言写成,极易移植.开放的源代码使得配置和裁剪方便,再加上体积小(所有二进制映像可仅20K).响应高速等特性 ...
- cocos2d怎么设置屏幕朝向?横屏 or 竖屏设置
在cocos引擎里面找了好久.没找到相关接口,网上也搜索了好久,最后发现.原来须要依据各个平台分别进行设置. android 改动项目根文件夹 proj.android\AndroidManifest ...
- springmvc+mybatis+maven项目框架搭建
项目的目录
- matlab获取图片的size属性,长宽
width=size(imread(‘文件名'),2): %获取图像宽length=size(imread(‘文件名'),1): %获取图像长 g=imread(['D:\文件及下载相关\桌面\代码 ...