Android高手進階教程(五)之—-Android 中LayoutInflater的使用!

來源:互聯網
上載者:User

大家好我們這一節講的是LayoutInflater的使用,在實際開發種LayoutInflater這個類還是非常有用的,它的作用類似於
findViewById(),

 

不同點是LayoutInflater是用來找layout下xml布局檔案,並且執行個體化!而findViewById()是找具體xml下的具體
widget控制項(如:Button,TextView等)。

 

為了讓大家容易理解我做了一個簡單的Demo,主布局main.xml裡有一個TextView和一個Button,當點擊Button,出現
Dialog,而這個Dialog的布局方式是我們在layout目錄下定義的custom_dialog.xml檔案(裡面左右分布,左邊
ImageView,右邊TextView)。

如下:

 

 

下面我將詳細的說明Demo的實現過程:

 

1、建立一個
Android工程,我們命名為LayoutInflaterDemo.

 

2、修改main.xml布局,裡面主要在
原來基礎上增加了一個Button.代碼如下:

 

<?xml version="1.0"<br />encoding="utf-8"?><br /><LinearLayout<br />xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="@string/hello"<br /> /><br /><Button<br />android:id="@+id/button"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />android:text="ShowCustomDialog"<br />/><br /></LinearLayout><br />

 

3.定義對話方塊的布局方式,我們在layout目錄下,建立一個名為
custom_dialog.xml檔案具體代碼如下:

 

<?xml version="1.0"<br />encoding="utf-8"?><br /><LinearLayout<br />xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:padding="10dp"<br /> ><br /> <ImageView android:id="@+id/image"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:layout_marginRight="10dp"<br /> /><br /> <TextView android:id="@+id/text"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:textColor="#FFF"<br /> /><br /></LinearLayout><br />

 

4.修改主程式LayouInflaterDemo.java代碼如下:

 

package com.android.tutor;<br />import android.app.Activity;<br />import android.app.AlertDialog;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.ImageView;<br />import android.widget.TextView;<br />public class LayoutInflaterDemo extends Activity implements<br />OnClickListener {</p><p>private Button button;<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> button = (Button)findViewById(R.id.button);<br /> button.setOnClickListener(this);<br /> }<br />@Override<br />public void onClick(View v) {</p><p>showCustomDialog();<br />}</p><p>public void showCustomDialog()<br />{<br />AlertDialog.Builder builder;<br />AlertDialog alertDialog;<br />Context mContext = LayoutInflaterDemo.this;</p><p>//下面倆種方法都可以<br />////LayoutInflater inflater = getLayoutInflater();<br />LayoutInflater inflater = (LayoutInflater)<br />mContext.getSystemService(LAYOUT_INFLATER_SERVICE);<br />View layout = inflater.inflate(R.layout.custom_dialog,null);<br />TextView text = (TextView) layout.findViewById(R.id.text);<br />text.setText("Hello, Welcome to Mr Wei's blog!");<br />ImageView image = (ImageView) layout.findViewById(R.id.image);<br />image.setImageResource(R.drawable.icon);<br />builder = new AlertDialog.Builder(mContext);<br />builder.setView(layout);<br />alertDialog = builder.create();<br />alertDialog.show();<br />}<br />}

 

5、最後執行之,點擊Button,將得到上述效果。

 

 好
今天就到此為止,睡覺了,大家有什麼不明白的請留言~謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.