通俗的说,inflate就相当于将一个xml中定义的布局找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.
因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(this, R.layout.dialog_layout, null);
TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.
相关推荐
LayoutInflater.from(context).inflate()方法的调研
主要介绍了Android LayoutInflater.inflate源码分析的相关资料,需要的朋友可以参考下
LayoutInflater在开发中使用频率很高,但是一直没有太知道LayoutInflater.from(context).inflate()的真正用法,今天就看看源码的流程。 首先来看from()的源码: /** * Obtains the LayoutInflater from the ...
博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965
return LayoutInflater.from(parent.getContext()).inflate(R.layout.view_calendar_item, parent, false); } // @Override public void setDate(View itemVew, int year, int month, int day, boolean ...
Android LayoutInflater.inflate...我尝试在Google官方文档与网络上其他讨论中寻找有关的说明,而后发现许多人不但不清楚LayoutInflater的inflate()方法的细节,而且甚至在误用它。 这里的困惑很大程度上是因为Google
主要介绍了Android 中LayoutInflater.inflate()方法的介绍的相关资料,希望通过本文大家能掌握这部分内容,需要的朋友可以参考下
VaryViewHepler切换不同布局的帮助类初始化helpermVaryViewHelper = new VaryViewHelper.Builder().setDataView(findViewById(R.id.vary_content)).setLoadingView(LayoutInflater.from(this).inflate(R.layout....
3.我们定义View的时候,如果需要在布局中使用,则必须实现带AttributeSet参数的构造方法,这又是为什么呢? 既然在这篇文章提出来,那说明这三个问题都是跟LayoutInflater脱不了干系的。在我们的分析过程中,会对...
LayoutInflater.from(this).inflate(R.layout.tab2, tabHost.getTabContentView(), true); LayoutInflater.from(this).inflate(R.layout.tab3, tabHost.getTabContentView(), true); //添加标签页 tabHost....
LayoutInflater源码分析 inflate方法的区别
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.p_template1,null); View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.p_template2,null); viewList.add(view1); ...
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.p_template1,null); View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.p_template2,null); viewList.add(view1); ...
mListView.addNoDataView(LayoutInflater.from(this).inflate(R.layout.header, null), Gravity.CENTER); // 此方法添加数据请求失败时显示的自定义视图 mListView.addNoLinkView(LayoutInflater.from(this...
如何使用View contentView = LayoutInflater.from(context).inflate(R.layout.fv_test,null); FloatWindow floatWindow = new FloatWindow.With(this,layo一个有用的类,它使创建浮动窗口更加容易。如何使用...
mHeadView = LayoutInflater.from(context).inflate(R.layout.head, null); mArrowImageView = (ImageView) mHeadView.findViewById(R.id.head_arrowImageView); mArrowImageView.setMinimumWidth(60); ...