最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開(kāi)發(fā)中setContentView和inflate的區(qū)別分析

 更新時(shí)間:2016年07月06日 10:15:01   作者:與時(shí)俱進(jìn)  
這篇文章主要介紹了Android開(kāi)發(fā)中setContentView和inflate的區(qū)別,較為詳細(xì)的分析了setContentView和inflate的功能、用法及二者的區(qū)別,需要的朋友可以參考下

本文實(shí)例講述了Android開(kāi)發(fā)中setContentView和inflate的區(qū)別。分享給大家供大家參考,具體如下:

一般用LayoutInflater做一件事:inflate

inflate這個(gè)方法總共有四種形式(見(jiàn)下面),目的都是把xml表述的layout轉(zhuǎn)化為View對(duì)象。

其中有一個(gè)比較常用,View inflate(int resource, ViewGroup root),另三個(gè),其實(shí)目的和這個(gè)差不多。

int resource,也就是resource/layout文件在R文件中對(duì)應(yīng)的ID,這個(gè)必須指定。

而ViewGroup root則可以是null,null時(shí)就只創(chuàng)建一個(gè)resource對(duì)應(yīng)的View,不是null時(shí),會(huì)將創(chuàng)建的view自動(dòng)加為root的child。

setContentView和inflate區(qū)別:

setContentView()一旦調(diào)用, layout就會(huì)立刻顯示UI;而inflate只會(huì)把Layout形成一個(gè)以view類實(shí)現(xiàn)成的對(duì)象,有需要時(shí)再用setContentView(view)顯示出來(lái)

一般在activity中通過(guò)setContentView()將界面顯示出來(lái),但是如果在非activity中如何對(duì)控件布局設(shè)置操作了,這需LayoutInflater動(dòng)態(tài)加載

<TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按鈕"
/>

在程序中動(dòng)態(tài)加載以上布局。

LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);

獲取布局中的控件。

button = (Button) view.findViewById(R.id.button);
textView = (TextView)view.findViewById(R.id.tview);

接下來(lái)結(jié)合源碼說(shuō)說(shuō)inflate方法的四種形式:

inflate方法總共有四種形式,把xml表達(dá)的layout轉(zhuǎn)化為view. This class is used to instantiate layout xml files into its corresponding view object. It is never be used directly——use getLayoutInflater() or getSystemService(String)getLayoutInflate() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up that is already hook up to the current context and correct configured for the device you are running on.

1. Context.public abstract object getSystemService(String name)

2. 兩種獲得LayoutInflater的方法

a. 通過(guò)SystemService獲得

復(fù)制代碼 代碼如下:
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);

b. 從給定的context中獲取

Public static LayoutInflater from(Context context)

c. 兩者的區(qū)別:實(shí)際上是一樣的,源碼

/**
   * 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;
}

3. LayoutInflater.inflate()將Layout文件轉(zhuǎn)換為View,專門(mén)供Layout使用的Inflater。雖然Layout也是View的子類,但在android中如果想將xml中的Layout轉(zhuǎn)換為View放入.java代碼中操作,只能通過(guò)Inflater,而不能通過(guò)findViewById()。

4.

LinearLayout linearLayout =
(LinearLayout) findViewById(R.id.placeslist_linearlayout);
linearLayout.addView(place_type_text);

5. findViewById有兩種形式

R.layout.xx是引用res/layout/xx.xml的布局文件(inflate 方法),R.id.xx是引用布局文件里面的組件,組件的id是xx(findViewById方法)。所有的組件id都能用R.id.xx來(lái)查看,但是組件不在setContentView()里面的layout中就無(wú)法使用,Activity.findViewById()會(huì)出現(xiàn)空指針異常

a. activity中的findViewById(int id)

b. View 中的findViewById(int id)

6.不同點(diǎn)是LayoutInflater是用來(lái)找layout下xml布局文件,并且實(shí)例化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android編程自定義Notification實(shí)例分析

    Android編程自定義Notification實(shí)例分析

    這篇文章主要介紹了Android編程自定義Notification的用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了自定義Notification的具體功能與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-12-12
  • android studio與手機(jī)連接調(diào)試步驟詳解

    android studio與手機(jī)連接調(diào)試步驟詳解

    這篇文章主要為大家詳細(xì)介紹了android studio與手機(jī)連接調(diào)試步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android ViewPager實(shí)現(xiàn)圖片輪播效果

    Android ViewPager實(shí)現(xiàn)圖片輪播效果

    這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)圖片輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)

    老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)

    下面小編就為大家?guī)?lái)一篇老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • android多種滑動(dòng)沖突的解決方案

    android多種滑動(dòng)沖突的解決方案

    本篇文章主要介紹了android多種滑動(dòng)沖突的解決方案,解決方案主要有2種,外部攔截法 和內(nèi)部攔截法,有興趣的可以了解一下。
    2017-02-02
  • android實(shí)現(xiàn)人臉識(shí)別技術(shù)的示例代碼

    android實(shí)現(xiàn)人臉識(shí)別技術(shù)的示例代碼

    本篇文章主要介紹了android人臉識(shí)別技術(shù)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Android圖片緩存之初識(shí)Glide(三)

    Android圖片緩存之初識(shí)Glide(三)

    這篇文章主要為大家詳細(xì)介紹了Android圖片緩存之Glide,學(xué)習(xí)比較優(yōu)秀的圖片緩存開(kāi)源框架,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Flutter控件之實(shí)現(xiàn)Widget基類的封裝

    Flutter控件之實(shí)現(xiàn)Widget基類的封裝

    在實(shí)際的開(kāi)發(fā)中,Widget的基類還是很有必要存在的,不然就會(huì)存在很多的冗余嵌套代碼,本文為大家介紹了Flutter中基類是如何封裝的,需要的可以收藏一下
    2023-05-05
  • MobPush?Android常見(jiàn)問(wèn)題解決分析

    MobPush?Android常見(jiàn)問(wèn)題解決分析

    這篇文章主要介紹了MobPush?Android常見(jiàn)問(wèn)題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Android 路徑查詢具體實(shí)現(xiàn)

    Android 路徑查詢具體實(shí)現(xiàn)

    可以通過(guò)RasterMap的getDirection()方法來(lái)查詢路徑,和查詢地址類似,路徑查詢的結(jié)果也是通過(guò)回調(diào)函數(shù)的方式來(lái)通知應(yīng)用程序的,下面的例子返回南京到北京的路徑
    2013-10-10

最新評(píng)論

格尔木市| 吉林市| 河南省| 民权县| 内乡县| 弥勒县| 嘉兴市| 射洪县| 民乐县| 井陉县| 咸宁市| 安多县| 金昌市| 长子县| 苏尼特右旗| 芷江| 铁力市| 兴山县| 惠水县| 申扎县| 开远市| 铜川市| 华蓥市| 南召县| 兴业县| 永修县| 西城区| 那曲县| 鹤岗市| 化隆| 温泉县| 东明县| 静海县| 定陶县| 青海省| 兴文县| 行唐县| 滕州市| 两当县| 普兰店市| 青龙|