亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Android 中的 LayoutInflater類

系統 2151 0
Inflater英文意思是膨脹,在android中大概是擴展的意思吧。
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種方法本質上是一樣的,看下面的源碼,form()調用的就是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的一個方法,根據傳入的NAME來取得對應的Object,然后轉換成相應的服務對象。以下介紹系統相應的服務。

傳入的Name 返回的對象 說明
WINDOW_SERVICE WindowManager 管理打開的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml里定義的view
ACTIVITY_SERVICE ActivityManager 管理應用程序的系統狀態
POWER_SERVICE PowerManger 電源的服務
ALARM_SERVICE AlarmManager 鬧鐘的服務
NOTIFICATION_SERVICE NotificationManager 狀態欄的服務
KEYGUARD_SERVICE KeyguardManager 鍵盤鎖的服務
LOCATION_SERVICE LocationManager 位置的服務,如GPS
SEARCH_SERVICE SearchManager 搜索的服務
VEBRATOR_SERVICE Vebrator 手機震動的服務
CONNECTIVITY_SERVICE Connectivity 網絡連接的服務
WIFI_SERVICE WifiManager Wi-Fi服務
TELEPHONY_SERVICE TeleponyManager 電話服務



    
//基本用法
public void showCustomDialog(){
  AlertDialog.Builder builder;
  AlertDialog alertDialog;
  Context mContext = AppActivity.this;
//下面倆種方法都可以
  //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 動態加載布局
http://labs.chinamobile.com/mblog/532767_72588?fdlayenxoaencysxyant
由于前段時間項目需要,需要在一個頁面上加載根據不同的按鈕加載不同的布局頁面,當時想到用 tabhot 。不過美工提供的界面圖完全用不上tabhot ,所以想到了動態加載的方法來解決這一需求。在這里我整理了一下,寫了一個 DEMO 希望大家以后少走點彎路。

首先,我們先把界面的框架圖畫出來,示意圖如下:


Android 中的 LayoutInflater類


中間白色部門是一個線性布局文件,我喜歡在畫圖的時候用不同的顏色將一塊布局標示出來,方便查看。布局文件代碼如下:
    
<?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>

  

從上面的效果圖可以看出,那塊白色的線性布局是用來動態加載傳進來的布局文件。好了,我們就來做如果把布局文件動態的加載進來。下面我們一步一步來實現這個效果,首先,先把需要的 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 每一行數據的樣式,代碼如下:
    <?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>
  



新建另外一個頁面,用來區分此頁面是動態加載的,代碼如下:
    <?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>
  


實現ListView 的添充數據,這里不詳細介紹如何填充ListView 每行數據,有不解的朋友可以回頭看我寫的文章:點擊這里 ,代碼如下:

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

}

  

項目大綱如下圖:

Android 中的 LayoutInflater類

好了,到此我們的準備工作就己經完成,接下來就是要教大家如何實現動態加載上面所畫的布局頁面了,先看一下效果圖:

點擊第一個按鈕:
Android 中的 LayoutInflater類

點擊第二個按鈕:
Android 中的 LayoutInflater類



動態加載代碼如下:
    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 方面里面的要放的布局文件里面,每次動態加載文件必需 調用 removeAllViews方法,清除之前的加載進來的 View 。是不是很簡單?當然動態加載VIEW 還有許多種方法,多嘗試不同寫法。可能會領會不一樣的心得,祝你早上掌握android 的開發技術。
Tip:因為是基于VIEW 操作,因此你可以用 Animation 的動畫效果使其更換界面更為自然,觀賞性更強。





Android 中的 LayoutInflater類


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!?。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 热99视频| 久久五月天婷婷 | 亚洲精品线在线观看 | 亚洲一区二区三区免费观看 | 国产女人伦码一区二区三区不卡 | 久久精品系列 | 日本三级带日本三级带黄首页 | 九九亚洲精品 | 欧美一区二区三区成人看不卡 | 999yy成年在线视频免费看 | 亚洲精品乱码久久久久久 | 视频福利在线 | 国产高清在线精品一区二区三区 | 成年女人18级毛片毛片免费观看 | 欧美性理论片在线观看片免费 | 精品久久国产老人久久综合 | 成人aaaa| 求毛片| 草草在线观看视频 | 一二三四社区在线播放 | 久久青青草原精品影院 | 国产高清美女一级a毛片久久 | 国内精品久久久久影院老司 | 国产精品久久久久影院色老大 | 曰本色wa | 在线 色 | 国产一区二区精品久久凹凸 | 久久亚洲综合伊人 | 午夜精品久久影院蜜桃 | 日本不卡免费 | 美女黄频视频大全免费高清 | 天天射天天干天天舔 | 国产成人综合久久亚洲精品 | 国产色| 天天干天天综合 | 日本不卡高清视频 | 免费的性生活视频 | 亚1洲二区三区四区免费 | 国产精品一区二区不卡 | 亚洲精品色一区色二区色三区 | 国产一级毛片在线 |