使用LayoutInflater动态加载布局和操作控件
2011-05-17 23:00
417 查看
我们知道在Android中通过布局文件来描述软件的界面,而通常在Activity中都是使用setContentView()来将布局显示出来。但是如果我们在非Activity的情况下,而且需要对布局中的控件进行设置等操作,该如何处理呢?这就需要使用到动态加载布局LayoutInflater,下面来做介绍。
以一个简单布局example.xml为例,里面只有一个按钮和一个文本显示框控件。
<TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按钮"
/>
在程序中动态加载以上布局。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
获取布局中的控件。
button = (Button) view.findViewById(R.id.button);
textView = (TextView)view.findViewById(R.id.tview);
为Button添加事件监听。
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
textView.setText("WWW.ATAAW.COM");
}
});
一般情况下,LayoutInflater在定义适配器中使用的比较多,例如我们可以为适配器定义布局,继而在适配器的设计中对控件进行数据绑定等设置操作。
本文出自 “android iOS” 博客,请务必保留此出处http://lovely1133.blog.51cto.com/1612511/567387
以一个简单布局example.xml为例,里面只有一个按钮和一个文本显示框控件。
<TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按钮"
/>
在程序中动态加载以上布局。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
获取布局中的控件。
button = (Button) view.findViewById(R.id.button);
textView = (TextView)view.findViewById(R.id.tview);
为Button添加事件监听。
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
textView.setText("WWW.ATAAW.COM");
}
});
一般情况下,LayoutInflater在定义适配器中使用的比较多,例如我们可以为适配器定义布局,继而在适配器的设计中对控件进行数据绑定等设置操作。
本文出自 “android iOS” 博客,请务必保留此出处http://lovely1133.blog.51cto.com/1612511/567387
相关文章推荐
- 使用LayoutInflater动态加载布局和操作控件
- 使用LayoutInflater动态加载布局和操作控件
- Android使用LayoutInflater动态加载布局和操作控件
- Android布局 使用LayoutInflater动态加载布局和操作控件
- android控件学习二:使用LayoutInflater动态加载布局和操作控件
- Android学习笔记31-使用惰性控件ViewStub实现布局动态加载
- 使用LayoutInflater动态加载布局和…
- 使用惰性控件ViewStub实现布局动态加载
- 使用惰性控件ViewStub实现布局动态加载
- Android学习笔记31:使用惰性控件ViewStub实现布局动态加载
- 委托delegate的使用-动态加载DLL控件
- ListView动态加载数据模板(使用代码布局)
- ListView动态加载数据模板(使用代码布局)
- 使用RenderControl方法实现动态加载用户控件
- android: 静态XML和动态加载XML混合使用,以及重写Layout控件
- Android自带抽屉布局及NavigationView的使用(动态加载头部图片,修改user名称)
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- 快速开发框架Afinal的使用(数据库操作,HTTP请求,网络图片加载,控件绑定)
- asp.net动态网站repeater控件使用及分页操作介绍
- 使用RenderControl方法实现动态加载用户控件