欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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轉化為View對象。

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

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

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

setContentView和inflate區(qū)別:

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

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

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

inflate方法總共有四種形式,把xml表達的layout轉化為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獲得

復制代碼 代碼如下:
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文件轉換為View,專門供Layout使用的Inflater。雖然Layout也是View的子類,但在android中如果想將xml中的Layout轉換為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等)

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

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

相關文章

  • Flutter 容器盒子模型的使用示例

    Flutter 容器盒子模型的使用示例

    在網(wǎng)頁開發(fā)中,有盒子模型,號稱統(tǒng)一三端的 Flutter 也不例外,而且和 HTML 的盒子模型幾乎是一樣的,本篇文章通過簡單的例子說明一下 Flutter 的盒子模型,方便以后再做界面時可以更好的理解布局。
    2021-05-05
  • Android實現(xiàn)左右擺動的球體動畫效果

    Android實現(xiàn)左右擺動的球體動畫效果

    這篇文章主要介紹了Android實現(xiàn)左右擺動的球體動畫效果,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Flutter系列重學Container示例詳解

    Flutter系列重學Container示例詳解

    這篇文章主要為大家介紹了Flutter系列重學Container示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Android編程基于距離傳感器控制手機屏幕熄滅的方法詳解

    Android編程基于距離傳感器控制手機屏幕熄滅的方法詳解

    這篇文章主要介紹了Android編程基于距離傳感器控制手機屏幕熄滅的方法,結合具體實例形式分析了Android距離傳感器的控制屏幕熄滅的實現(xiàn)方法與相關操作技巧,需要的朋友可以參考下
    2017-11-11
  • Android自定義PopWindow實現(xiàn)QQ、微信彈出菜單效果

    Android自定義PopWindow實現(xiàn)QQ、微信彈出菜單效果

    這篇文章主要為大家詳細介紹了Android自定義PopWindow實現(xiàn)QQ、微信彈出菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 深入分析Android構建過程

    深入分析Android構建過程

    本篇內容主要講了Android 構建的過程需要的步驟以及從編譯到生成等原理,一起來學習下。
    2017-11-11
  • 實例講解Android Fragment的兩種使用方法

    實例講解Android Fragment的兩種使用方法

    今天小編就為大家分享一篇關于實例講解Android Fragment的兩種使用方法,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Android實現(xiàn)圖片上傳功能

    Android實現(xiàn)圖片上傳功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)圖片上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android控件拖動實例詳解

    Android控件拖動實例詳解

    這篇文章主要介紹了 Android控件的拖動實例詳解的相關資料,這里附有實例代碼,幫助大家學習理解,需要的朋友可以參考下
    2016-12-12
  • Flutter利用ORM框架管理數(shù)據(jù)庫詳解

    Flutter利用ORM框架管理數(shù)據(jù)庫詳解

    使用?ORM?框架最大的好處是簡化了數(shù)據(jù)庫維護的代碼量,使得我們可以專注于業(yè)務代碼實現(xiàn)。本篇,我們看看如何使用ORM框架管理數(shù)據(jù)庫版本遷移,需要的可以參考一下
    2023-04-04

最新評論