我们知道在Android中通过布局文件来描述软件的界面,而通常在Activity中都是使用setContentView()来将布局显示出来。但是如果我们在非Activity的情况下,而且需要对布局中的控件进行设置等操作,该如何处理呢?这就需要使用到动态加载布局LayoutInflater,下面ATAAW.COM来做介绍。
以一个简单布局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在定义适配器中使用的比较多,例如我们可以为适配器定义布局,继而在适配器的设计中对控件进行数据绑定等设置操作。
Posted in 软件开发 Tags: Activity, Button, UI, 控件, 界面, 程序, 设置, 适配器
分享到:
相关推荐
LayoutInflater源码分析 inflate方法的区别
主要介绍了Android LayoutInflater分析的相关资料,需要的朋友可以参考下
Android 中LayoutInflater的使用
LayoutInflater Layout 与 Inflater[扩充]两个单词的合并,可以理解为:当前Activity 布局的扩充.
通过LayoutInflater.Factory实现主题切换功能,详见:http://blog.csdn.net/llew2011
Android 中LayoutInflater(布局加载器)之实战篇 博客的Demo 博客地址: http://blog.csdn.net/l540675759/article/details/78112989 两种方式实现小红书的引导页: (1)自定义View (2)自定义LayoutInflater....
NULL 博文链接:https://lxmgfd.iteye.com/blog/979716
LayoutInflater.from(context).inflate()方法的调研
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
在android中,LayoutInflater有点类似于Activity的findViewById(id),不同的是LayoutInflater是用来找layout下的xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。...
NULL 博文链接:https://yufeng-zhu.iteye.com/blog/1676207
android中LayoutInflater的使用.pdf
JavaScript应用实例-LayoutInflater.js
Android LayoutInflater加载布局详解 对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? ...
down-test-Android 获得 LayoutInflater 实例的三种方式
在平时的开发过程中,我们经常会用LayoutInflater这个类,比如说在Fragment$onCreateView和RecyclerView.Adapter$onCreateViewHolder中都会用到。它的用法也无非就是LayoutInflater.inflate(resourceId, root, ...
这是老外写的经典Android代码,对学习AsyncTask、JSONAdapter、LayoutInflater很有用