国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之獲取LayoutInflater對象的方法總結

2020-04-11 10:49:25
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發之獲取LayoutInflater對象的方法。分享給大家供大家參考,具體如下:

在寫Android程序時,有時候會編寫自定義的View,使用Inflater對象來將布局文件解析成一個View。本文主要目的是總結獲取LayoutInflater對象的方法。

1、若能獲取context對象,可以有以下幾種方法:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View child = inflater.inflate(R.layout.child, null);

或者:

LayoutInflater inflater = LayoutInflater.from(context);View child = inflater.inflate(R.layout.child, null);

2、在一個Activity中,可以有以下方法:

View child = getLayoutInflater().inflate(R.layout.child, item, false);

或者:

View view;LayoutInflater inflater = (LayoutInflater)  getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);view = inflater.inflate(R.layout.mylayout, null);

方法1和方法2其實都是對context().getSystemService()的使用

3、使用View的靜態方法:

View view=View.inflate(context, R.layout.child, null)

inflate實現源碼如下:

/** * Inflate a view from an XML resource. This convenience method wraps the {@link * LayoutInflater} class, which provides a full range of options for view inflation. * * @param context The Context object for your activity or application. * @param resource The resource ID to inflate * @param root A view group that will be the parent. Used to properly inflate the * layout_* parameters. * @see LayoutInflater */public static View inflate(Context context, int resource, ViewGroup root) {  LayoutInflater factory = LayoutInflater.from(context);  return factory.inflate(resource, root);}

LayoutInflater.from(context)實際上是對方法1的包裝,可參考以下源碼:

/** * Obtains the LayoutInflater from the given 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相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android控件用法總結》、《Android短信與電話操作技巧匯總》及《Android多媒體操作技巧匯總(音頻,視頻,錄音等)

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永嘉县| 武定县| 游戏| 凤山县| 翼城县| 府谷县| 万载县| 察雅县| 江城| 新源县| 达拉特旗| 泽普县| 定兴县| 永昌县| 宁化县| 华池县| 麻栗坡县| 黄冈市| 石楼县| 东兴市| 永年县| 福清市| 荣昌县| 刚察县| 玉溪市| 阿坝| 汾阳市| 丽水市| 平乡县| 南澳县| 美姑县| 改则县| 衢州市| 得荣县| 蒲江县| 满城县| 仙居县| 泰安市| 时尚| 深州市| 松原市|