android inflate,Android 关于inflate
通俗的說,inflate就相當于將一個xml中定義的布局找出來.
因為在一個Activity里如果直接用findViewById()的話,對應的是setConentView()的那個layout里的組件.
因此如果你的Activity里如果用到別的layout,比如對話框上的layout,你還要設置對話框上的layout里的組件(像圖片ImageView,文字TextView)上的內容,你就必須用inflate()先將對話框上的layout找出來,然后再用這個layout對象去找到它上面的組件,如:
View view=View.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:
LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后調用inflate方法將xml布局文件轉成View
public View inflate(intresource,ViewGroup root, boolean attachToRoot)
在View類中,也有inflate方法
public static View inflate(Context context , int resource,ViewGroup root)
總結
以上是生活随笔為你收集整理的android inflate,Android 关于inflate的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中List和Map接口之间的区别
- 下一篇: string中concat_JavaSc