Android 之 LayoutInflater
16lz
2021-01-23
import android.view.LayoutInflater;
它的作用就是这样地:
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog,null); TextView text = (TextView) layout.findViewById(R.id.text);
其实,它的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
获取LayoutInflater的方式有以下几种:
【1】
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
【2】
LayoutInflater inflater = getLayoutInflater();inflater = LayoutInflater.from(context);
实质以上两种是一样的:
public static LayoutInflater from(Context context) { LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (LayoutInflater == null) { throw new AssertionError("LayoutInflater not found."); } return LayoutInflater; }
更多相关文章
- android中showSoftInput不起作用
- 在Android 7.0上PopupWindow.showAsDropDown不起作用的解决方法
- android中.classpath和.project作用
- Linux文件的特殊权限位SUID、SGID作用及编程设置/读取
- 关于Android的selector背景选择器的配置不起作用的问题
- Android环境变量作用--命令行操作(ADB、AVD等)
- Android ImageView控件的MaxWidth、MaxHeight不起作用