天天看點

自定義對話框dialog的建立

自定義dialog的建立

如果你想自定義dialog的布局,你可以自己建立一個dialog布局。定義好之後,傳遞根View對象或者資源ID到setContextView(View)方法。

例如,如上圖的dialog:

1-建立一個xml布局檔案custom_dialog.xml;

view plaincopy to clipboardprint?

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

              android:id="@+id/layout_root" 

              android:orientation="horizontal" 

              android:layout_width="fill_parent" 

              android:layout_height="fill_parent" 

              android:padding="10dp" 

              >  

    <ImageView android:id="@+id/image" 

               android:layout_width="wrap_content" 

               android:layout_height="fill_parent" 

               android:layout_marginRight="10dp" 

               />  

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

              android:layout_width="wrap_content" 

              android:layout_height="fill_parent" 

              android:textColor="#FFF" 

              />  

</LinearLayout> 

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

              android:id="@+id/layout_root"

              android:orientation="horizontal"

              android:layout_width="fill_parent"

              android:layout_height="fill_parent"

              android:padding="10dp"

              >

    <ImageView android:id="@+id/image"

               android:layout_width="wrap_content"

               android:layout_height="fill_parent"

               android:layout_marginRight="10dp"

               />

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

              android:layout_width="wrap_content"

              android:layout_height="fill_parent"

              android:textColor="#FFF"

              />

</LinearLayout>

這個xml在LinearLayout裡定義了一個ImageView和TextView。

2-設定上面的布局為dialog的context view ,并且定義ImageView和TextView兩個元素。

view plaincopy to clipboardprint?

Context mContext = getApplicationContext();  

Dialog dialog = new Dialog(mContext);  

dialog.setContentView(R.layout.custom_dialog);  

dialog.setTitle("Custom Dialog");  

TextView text = (TextView) dialog.findViewById(R.id.text);  

text.setText("Hello, this is a custom dialog!");  

ImageView image = (ImageView) dialog.findViewById(R.id.image);  

image.setImageResource(R.drawable.android); 

Context mContext = getApplicationContext();

Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);

dialog.setTitle("Custom Dialog");

TextView text = (TextView) dialog.findViewById(R.id.text);

text.setText("Hello, this is a custom dialog!");

ImageView image = (ImageView) dialog.findViewById(R.id.image);

image.setImageResource(R.drawable.android);

執行個體化dialog後,使用setContextView(int)方法設定自定義的布局。現在dialog便有了一個自定義的布局,你可以使用findViewById(int)方法來獲得或者修改布局。

3-完成了,現在你可以顯示自定義的dialog了。

一個dialog必須有一個title,如果你沒有調用setTitile()方法,那麼會标題處會顯示空,但dialog仍然可見,如果你不想顯示标題,隻有寫一個自己的dialog類了。然而,因為一個AlertDialog使用AlertDialog.builder類建立起來非常簡單,你不必使用setContextView(int)方法。但必須使用setView(view)方法代替。這個方法會接受一個view參數,你需要從xml中得到根view元素。

得到xml布局,通過LayoutInflater類的getLayoutflater()方法(或者getSystemService()方法),然後調用inflate(int,ViewGroup)方法,第一個參數是xml檔案id,第二個參數是根view的id,在這點上,你可以使用inflated 布局來獲得xml中的view對象并且定義ImageView和TextView對象,然後執行個體化AlertDialog.Builder類并且使用setView(View)方法來設定布局。

這有一個自定義dialog布局檔案的例子:

view plaincopy to clipboardprint?

AlertDialog.Builder builder;  

AlertDialog alertDialog;  

Context mContext = getApplicationContext();  

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

View layout = inflater.inflate(R.layout.custom_dialog,  

                               (ViewGroup) findViewById(R.id.layout_root));  

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

text.setText("Hello, this is a custom dialog!");  

ImageView image = (ImageView) layout.findViewById(R.id.image);  

image.setImageResource(R.drawable.android);  

builder = new AlertDialog.Builder(mContext);  

builder.setView(layout);  

alertDialog = builder.create(); 

AlertDialog.Builder builder;

AlertDialog alertDialog;

Context mContext = getApplicationContext();

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

View layout = inflater.inflate(R.layout.custom_dialog,

                               (ViewGroup) findViewById(R.id.layout_root));

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

text.setText("Hello, this is a custom dialog!");

ImageView image = (ImageView) layout.findViewById(R.id.image);

image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);

builder.setView(layout);

alertDialog = builder.create();

使用自定義布局這種方式來生成dialog,可以讓你使用更進階的特性,比如管理按鈕、清單、标題、圖示等。

本文來自CSDN部落格,轉載請标明出處:http://blog.csdn.net/iamlazybone/archive/2010/10/02/5919094.aspx