Inflater英文意思是膨脹,在android中大概是擴展的意思吧。
LayoutInflater的作用類似于 findViewById(),不同點是LayoutInflater是用來找layout下xml布局文件,并且實例化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)。
它的用法有2種:
另一種方法:
上面2種方法本質(zhì)上是一樣的,看下面的源碼,form()調(diào)用的就是getSystemService():
另外getSystemService()是Android很重要的一個API,它是Activity的一個方法,根據(jù)傳入的NAME來取得對應的Object,然后轉(zhuǎn)換成相應的服務對象。以下介紹系統(tǒng)相應的服務。
Android 動態(tài)加載布局
http://labs.chinamobile.com/mblog/532767_72588?fdlayenxoaencysxyant
由于前段時間項目需要,需要在一個頁面上加載根據(jù)不同的按鈕加載不同的布局頁面,當時想到用 tabhot 。不過美工提供的界面圖完全用不上tabhot ,所以想到了動態(tài)加載的方法來解決這一需求。在這里我整理了一下,寫了一個 DEMO 希望大家以后少走點彎路。
首先,我們先把界面的框架圖畫出來,示意圖如下:
中間白色部門是一個線性布局文件,我喜歡在畫圖的時候用不同的顏色將一塊布局標示出來,方便查看。布局文件代碼如下:
從上面的效果圖可以看出,那塊白色的線性布局是用來動態(tài)加載傳進來的布局文件。好了,我們就來做如果把布局文件動態(tài)的加載進來。下面我們一步一步來實現(xiàn)這個效果,首先,先把需要的 XML? 勾畫出來,分為步驟如下。
新建一個布局用來存放 ListView 頁面,代碼如下:
新建一個 ListView 每一行數(shù)據(jù)的樣式,代碼如下:
新建另外一個頁面,用來區(qū)分此頁面是動態(tài)加載的,代碼如下:
實現(xiàn)ListView 的添充數(shù)據(jù),這里不詳細介紹如何填充ListView 每行數(shù)據(jù),有不解的朋友可以回頭看我寫的文章:點擊這里 ,代碼如下:
項目大綱如下圖:
好了,到此我們的準備工作就己經(jīng)完成,接下來就是要教大家如何實現(xiàn)動態(tài)加載上面所畫的布局頁面了,先看一下效果圖:
點擊第一個按鈕:
點擊第二個按鈕:
動態(tài)加載代碼如下:
上面通過使用LayoutInflater? 每次點擊按鈕時候去讀取布局文件,然后找到布局文件里面的各個VIEW 操作完VIEW 后加載進我們setContentView 方面里面的要放的布局文件里面,每次動態(tài)加載文件必需 調(diào)用 removeAllViews方法,清除之前的加載進來的 View 。是不是很簡單?當然動態(tài)加載VIEW 還有許多種方法,多嘗試不同寫法。可能會領(lǐng)會不一樣的心得,祝你早上掌握android 的開發(fā)技術(shù)。
Tip:因為是基于VIEW 操作,因此你可以用 Animation 的動畫效果使其更換界面更為自然,觀賞性更強。
LayoutInflater的作用類似于 findViewById(),不同點是LayoutInflater是用來找layout下xml布局文件,并且實例化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)。
它的用法有2種:
LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null); 或者干脆并成一句: View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
另一種方法:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view=inflater.inflate(R.layout.ID, null);
上面2種方法本質(zhì)上是一樣的,看下面的源碼,form()調(diào)用的就是getSystemService():
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; }
另外getSystemService()是Android很重要的一個API,它是Activity的一個方法,根據(jù)傳入的NAME來取得對應的Object,然后轉(zhuǎn)換成相應的服務對象。以下介紹系統(tǒng)相應的服務。
傳入的Name | 返回的對象 | 說明 |
WINDOW_SERVICE | WindowManager | 管理打開的窗口程序 |
LAYOUT_INFLATER_SERVICE | LayoutInflater | 取得xml里定義的view |
ACTIVITY_SERVICE | ActivityManager | 管理應用程序的系統(tǒng)狀態(tài) |
POWER_SERVICE | PowerManger | 電源的服務 |
ALARM_SERVICE | AlarmManager | 鬧鐘的服務 |
NOTIFICATION_SERVICE | NotificationManager | 狀態(tài)欄的服務 |
KEYGUARD_SERVICE | KeyguardManager | 鍵盤鎖的服務 |
LOCATION_SERVICE | LocationManager | 位置的服務,如GPS |
SEARCH_SERVICE | SearchManager | 搜索的服務 |
VEBRATOR_SERVICE | Vebrator | 手機震動的服務 |
CONNECTIVITY_SERVICE | Connectivity | 網(wǎng)絡連接的服務 |
WIFI_SERVICE | WifiManager | Wi-Fi服務 |
TELEPHONY_SERVICE | TeleponyManager | 電話服務 |
//基本用法 public void showCustomDialog(){ AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = AppActivity.this; //下面?zhèn)z種方法都可以 //LayoutInflater inflater = getLayoutInflater(); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog,null); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello, Welcome to Mr Wei's blog!"); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.icon); builder = new AlertDialog.Builder(mContext); builder.setView(layout); alertDialog = builder.create(); alertDialog.show(); } } protected void showToast(int type) { Toast.makeText(this, "*********", Toast.LENGTH_LONG).show(); LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = li.inflate(R.layout.toast, null); Toast toast = new Toast(this); toast.setView(view); toast.setDuration(type); toast.show(); }
Android 動態(tài)加載布局
http://labs.chinamobile.com/mblog/532767_72588?fdlayenxoaencysxyant
由于前段時間項目需要,需要在一個頁面上加載根據(jù)不同的按鈕加載不同的布局頁面,當時想到用 tabhot 。不過美工提供的界面圖完全用不上tabhot ,所以想到了動態(tài)加載的方法來解決這一需求。在這里我整理了一下,寫了一個 DEMO 希望大家以后少走點彎路。
首先,我們先把界面的框架圖畫出來,示意圖如下:

中間白色部門是一個線性布局文件,我喜歡在畫圖的時候用不同的顏色將一塊布局標示出來,方便查看。布局文件代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:text="加載ListView" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <Button android:text="加載另外一個頁面" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout01" android:background="#FFFFFF" android:layout_width="fill_parent" android:layout_height="fill_parent"></LinearLayout> </LinearLayout>
從上面的效果圖可以看出,那塊白色的線性布局是用來動態(tài)加載傳進來的布局文件。好了,我們就來做如果把布局文件動態(tài)的加載進來。下面我們一步一步來實現(xiàn)這個效果,首先,先把需要的 XML? 勾畫出來,分為步驟如下。
新建一個布局用來存放 ListView 頁面,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView> </LinearLayout>
新建一個 ListView 每一行數(shù)據(jù)的樣式,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout>
新建另外一個頁面,用來區(qū)分此頁面是動態(tài)加載的,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:id="@+id/hellolayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:text="HELLO" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout>
實現(xiàn)ListView 的添充數(shù)據(jù),這里不詳細介紹如何填充ListView 每行數(shù)據(jù),有不解的朋友可以回頭看我寫的文章:點擊這里 ,代碼如下:
package com.terry; import java.util.ArrayList; import java.util.HashMap; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class listAdapter extends BaseAdapter { ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); private LayoutInflater inflater; public listAdapter(Context contex) { inflater=LayoutInflater.from(contex); HashMap<String, Object> map=new HashMap<String, Object>(); for (int i = 0; i < 10; i++) { map.put("name", "例子"); list.add(map); } } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final viewHolder myHolder; if (convertView==null) { myHolder=new viewHolder(); convertView=inflater.inflate(R.layout.list_view_row, null); myHolder.tv=(TextView)convertView.findViewById(R.id.TextView01); convertView.setTag(myHolder); } else { myHolder=(viewHolder)convertView.getTag(); } myHolder.tv.setText(list.get(position).get("name").toString()); return convertView; } }
項目大綱如下圖:

好了,到此我們的準備工作就己經(jīng)完成,接下來就是要教大家如何實現(xiàn)動態(tài)加載上面所畫的布局頁面了,先看一下效果圖:
點擊第一個按鈕:

點擊第二個按鈕:

動態(tài)加載代碼如下:
package com.terry; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; public class dynaActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final LayoutInflater inflater = LayoutInflater.from(this); Button btn = (Button) findViewById(R.id.Button01); Button btn2 = (Button) findViewById(R.id.Button02); final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LinearLayout layout = (LinearLayout) inflater.inflate( R.layout.listview, null).findViewById(R.id.layout); ListView lv=(ListView)layout.getChildAt(0); lv.setAdapter(new listAdapter(dynaActivity.this)); lin.removeAllViews(); lin.addView(layout); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LinearLayout layout = (LinearLayout) inflater.inflate( R.layout.hello, null).findViewById(R.id.hellolayout); TextView lv=(TextView)layout.getChildAt(0); lv.setTextColor(Color.RED); lin.removeAllViews(); lin.addView(layout); } }); } }
上面通過使用LayoutInflater? 每次點擊按鈕時候去讀取布局文件,然后找到布局文件里面的各個VIEW 操作完VIEW 后加載進我們setContentView 方面里面的要放的布局文件里面,每次動態(tài)加載文件必需 調(diào)用 removeAllViews方法,清除之前的加載進來的 View 。是不是很簡單?當然動態(tài)加載VIEW 還有許多種方法,多嘗試不同寫法。可能會領(lǐng)會不一樣的心得,祝你早上掌握android 的開發(fā)技術(shù)。
Tip:因為是基于VIEW 操作,因此你可以用 Animation 的動畫效果使其更換界面更為自然,觀賞性更強。
更多文章、技術(shù)交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
