ndroid中View.inflate()
来源:互联网 发布:js push json 编辑:程序博客网 时间:2024/05/29 14:39
Inflate可用于将一个xml中定义的布局控件找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件。因此如果当前Activity里需要用到别的layout(xml文件),比如对话框上的layout,这时还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,所以,就必须用inflate()先将对话框的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
View view=View.inflate(this,R.layout.dialog_layout,null);
TextViewdialogTV=(TextView)view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
如果直接用this.findViewById(R.id.dialog_tv)肯定会报错.
生成LayoutInflater的方法:
上文示例中写的方法来创建inflate一般不常用,一般通过LayoutInflater来创建inflate。主要有三种方式可以生成LayoutInflater:
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
LayoutInflater inflater = LayoutInflater.from(context);
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件。因此如果当前Activity里需要用到别的layout(xml文件),比如对话框上的layout,这时还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,所以,就必须用inflate()先将对话框的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
View view=View.inflate(this,R.layout.dialog_layout,null);
TextViewdialogTV=(TextView)view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
如果直接用this.findViewById(R.id.dialog_tv)肯定会报错.
生成LayoutInflater的方法:
上文示例中写的方法来创建inflate一般不常用,一般通过LayoutInflater来创建inflate。主要有三种方式可以生成LayoutInflater:
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
LayoutInflater inflater = LayoutInflater.from(context);
阅读全文
0 0
- ndroid中View.inflate()
- Android中View.inflate()
- View中inflate方法
- Android中通过inflate构建一个View
- View.inflate(Context,layout,null)中null的含义
- 关于View.inflate()?
- View.inflate 和 inflater.inflate 区别
- View.Inflate)与LayoutInflater.from().inflate();
- View.inflate()与LayoutInflater.inflate()的区别?
- ndroid中BitmapFactory.Options详解
- 利用View.inflate加载xml
- LayoutInflater.from(this) View.inflate()
- 利用View.inflate加载xml
- LayoutInflater.from(this) View.inflate()
- android:打气筒功能View.inflate
- LayoutInflater.inflate方法加载View
- ndroid中ImageView的ScaleType属性
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- HashMap的实现
- AngualrJs 最新 文件图片上传教程5:Client-css Client-config
- LeetCode问题解答:19. Remove Nth Node From End of List
- Unity资源加载以及释放
- 强制横屏或竖屏
- ndroid中View.inflate()
- Java面向对象(四)final关键字
- Linux+C+动态库(*.so文件)
- DbComparisonExpression需要具有可比较类型的参数。
- Ajax接收Java服务端发送的text数据(用户名检查)
- @Column 大小写
- android下如何把触摸屏的驱动动态加载
- 常见的局域网拓扑结构
- 总结