欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android的LayoutInflater-創(chuàng)新互聯(lián)

在 實(shí)際開發(fā)中LayoutInflater這個(gè)類還是非常有用的,它的作用類似于findViewById()。不同點(diǎn)是LayoutInflater是用 來(lái)找res/layout/下的xml布局文件,并且實(shí)例化;而findViewById()是找xml布局文件下的具體widget控件(如 Button、TextView等)。
具體作用:
1、對(duì)于一個(gè)沒(méi)有被載入或者想要?jiǎng)討B(tài)載入的界面,都需要使用LayoutInflater.inflate()來(lái)載入;

2、對(duì)于一個(gè)已經(jīng)載入的界面,就可以使用Activiyt.findViewById()方法來(lái)獲得其中的界面元素。

LayoutInflater 是一個(gè)抽象類,在文檔中如下聲明:

publicabstractclass LayoutInflater extends Object

獲得 LayoutInflater 實(shí)例的三種方式

1.LayoutInflater inflater = getLayoutInflater();  //調(diào)用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService

                                (Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater = LayoutInflater.from(context);

其實(shí),這三種方式本質(zhì)是相同的,從源碼中可以看出:這三種方式最終本質(zhì)是都是調(diào)用的Context.getSystemService()。

下面是一個(gè)Demo

成都創(chuàng)新互聯(lián)公司從2013年成立,先為唐縣等服務(wù)建站,唐縣等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為唐縣企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
  1. main.xml

  2. <?xml version="1.0"

  3. encoding="utf-8"?>

  4. <LinearLayout

  5. xmlns:android="http://schemas.android.com/apk/res/android"

  6.   android:orientation="vertical"

  7.   android:layout_width="fill_parent"

  8.   android:layout_height="fill_parent"

  9.   >

  10. <TextView

  11.   android:layout_width="fill_parent"

  12.   android:layout_height="wrap_content"

  13.   android:text="@string/hello"

  14.   />

  15. <Button

  16.   android:id="@+id/button"

  17.   android:layout_width="wrap_content"

  18.   android:layout_height="wrap_content"

  19.   android:text="ShowCustomDialog"

  20.   />

  21. </LinearLayout>

復(fù)制代碼

定義對(duì)話框的布局方式custom_dialog.xml

  1. <?xml version="1.0"

  2. encoding="utf-8"?>

  3. <LinearLayout

  4. xmlns:android="http://schemas.android.com/apk/res/android"

  5.           android:orientation="horizontal"

  6.           android:layout_width="fill_parent"

  7.           android:layout_height="fill_parent"

  8.           android:padding="10dp"

  9.           >

  10.   <ImageView android:id="@+id/p_w_picpath"

  11.           android:layout_width="wrap_content"

  12.           android:layout_height="fill_parent"

  13.           android:layout_marginRight="10dp"

  14.           />

  15.   <TextView android:id="@+id/text"

  16.           android:layout_width="wrap_content"

  17.           android:layout_height="fill_parent"

  18.           android:textColor="#FFF"

  19.           />

  20. </LinearLayout>

復(fù)制代碼

Activity代碼

  1. package com.android.tutor;

  2. import android.app.Activity;

  3. import android.app.AlertDialog;

  4. import android.content.Context;

  5. import android.os.Bundle;

  6. import android.view.LayoutInflater;

  7. import android.view.View;

  8. import android.view.View.OnClickListener;

  9. import android.widget.Button;

  10. import android.widget.ImageView;

  11. import android.widget.TextView;

  12. public class LayoutInflaterDemo extends Activity implements

  13. OnClickListener {

  14. private Button button;

  15.   public void onCreate(Bundle savedInstanceState) {

  16.       super.onCreate(savedInstanceState);

  17.       setContentView(R.layout.main);

  18.       button = (Button)findViewById(R.id.button);

  19.       button.setOnClickListener(this);

  20.   }

  21. @Override

  22. public void onClick(View v) {

  23.   showCustomDialog();

  24. }

  25. public void showCustomDialog()

  26. {

  27.   AlertDialog.Builder builder;

  28.   AlertDialog alertDialog;

  29.   Context mContext = LayoutInflaterDemo.this;

  30.   //下面?zhèn)z種方法都可以

  31.   //LayoutInflater inflater = getLayoutInflater();

  32.   LayoutInflater inflater = (LayoutInflater)  mContext.getSystemService(LAYOUT_INFLATER_SERVICE);

  33.   View layout = inflater.inflate(R.layout.custom_dialog,null);  //返回值為view

  34.   TextView text = (TextView) layout.findViewById(R.id.text);

  35.   text.setText("Hello, Welcome to Mr Wei's blog!");

  36.   ImageView p_w_picpath = (ImageView) layout.findViewById(R.id.p_w_picpath);

  37.   p_w_picpath.setImageResource(R.drawable.icon);

  38.   builder = new AlertDialog.Builder(mContext);

  39.   builder.setView(layout);

  40.   alertDialog = builder.create();

  41.   alertDialog.show();

  42. }

  43. }

復(fù)制代碼

運(yùn)行效果:

Android的LayoutInflater

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)頁(yè)名稱:Android的LayoutInflater-創(chuàng)新互聯(lián)
文章路徑:http://chinadenli.net/article12/hjedc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序網(wǎng)站制作商城網(wǎng)站App設(shè)計(jì)面包屑導(dǎo)航移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)