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

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

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

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

一般用LayoutInflater做一件事:inflate

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

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

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

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

setContentView和inflate區(qū)別:

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

一般在activity中通過setContentView()將界面顯示出來,但是如果在非activity中如何對控件布局設(shè)置操作了,這需LayoutInflater動態(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="按鈕"
/>

在程序中動態(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);

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

inflate方法總共有四種形式,把xml表達的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. 通過SystemService獲得

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

b. 從給定的context中獲取

Public static LayoutInflater from(Context context)

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

/**
   * 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,專門供Layout使用的Inflater。雖然Layout也是View的子類,但在android中如果想將xml中的Layout轉(zhuǎn)換為View放入.java代碼中操作,只能通過Inflater,而不能通過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來查看,但是組件不在setContentView()里面的layout中就無法使用,Activity.findViewById()會出現(xiàn)空指針異常

a. activity中的findViewById(int id)

b. View 中的findViewById(int id)

6.不同點是LayoutInflater是用來找layout下xml布局文件,并且實例化!而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ù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

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

相關(guān)文章

  • Android編程自定義Notification實例分析

    Android編程自定義Notification實例分析

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

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

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

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

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

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

    下面小編就為大家?guī)硪黄仙U凩istview中onItemClick中的各個參數(shù)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • android多種滑動沖突的解決方案

    android多種滑動沖突的解決方案

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

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

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

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

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

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

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

    MobPush?Android常見問題解決分析

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

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

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

最新評論

游戏| 冕宁县| 隆化县| 博乐市| 横山县| 邢台市| 维西| 固阳县| 大宁县| 确山县| 长泰县| 郑州市| 平定县| 平定县| 横山县| 和顺县| 越西县| 霍州市| 临安市| 环江| 昌邑市| 镇安县| 长子县| 分宜县| 葫芦岛市| 万载县| 略阳县| 清苑县| 普兰店市| 泾川县| 武川县| 古交市| 新绛县| 皋兰县| 汉阴县| 柳江县| 凤城市| 大连市| 阳原县| 和田市| 郯城县|