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 ...
随机推荐
- 转:Andriod studio技巧合集
1. 书签(Bookmarks) 描述:这是一个很有用的功能,让你可以在某处做个标记(书签),方便后面再跳转到此处. 调用:Menu → Navigate → Bookmarks 快捷键: 添加/移除 ...
- .NET Standard - 揭秘 .NET Core 和 .NET Standard[转自MSDN]
作为 .NET 系列的最新成员,.NET Core 和 .NET Standard 的概念及其与 .NET Framework 的区别并不十分明确.在本文中,我将准确介绍每个产品及其适用场景. 在详细 ...
- C语言 · P1001(大数乘法)
算法提高 P1001 时间限制:1.0s 内存限制:256.0MB 当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法.具体 ...
- exec系列函数(execl,execlp,execle,execv,execvp)使用
本节目标: exec替换进程映像 exec关联函数组(execl.execlp.execle.execv.execvp) 一,exec替换进程映像 在进程的创建上Unix采用了一个独特的方法,它将进程 ...
- PHP——字符串
<?php //strlen("aaa");取字符串的长度 *** echo strlen("aaaaa"); echo "<br /&g ...
- 修复安卓的bug
一直不明白listview中的复用,为什么会出现,项目多了.点击同一行的按钮,操作的不是指定行的对象. 仔细研读了代码,突然明白了.因为复用了,导致了position改变了. 伪码 if(conver ...
- linux 链接的使用 创建和删除符号连接(软、硬链接)
1 . 使用方式 :ln [option] source_file dist_file (source_file是待建立链接文件的文件,dist_file是新创建的链接文件) ...
- 《C++ Primer》笔记-#include,#ifndef
1.理解 #include 指示是怎样工作的 #include 设 施是 C++ 预处理器的一部分.预处理器处理程序的源代码,在编译器之前运行. C++ 继承了 C 的非常精细的预处理器.现在的 C+ ...
- 使用jQuery模拟Google的自动提示效果
注意: 1.本功能使用SqlServler2000中的示例数据库Northwind,请打SP3或SP4补丁:2.请下载jQuery组件,河西FTP中有下载:3.本功能实现类似Google自动提示的效果 ...
- 【Debian】时间设置
http://blog.linuxphp.org/archives/567/ http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0819/ ...