现在的位置: 首页 > 综合 > 正文

Android 对话框(三)自定义对话框

2013年08月12日 ⁄ 综合 ⁄ 共 5250字 ⁄ 字号 评论关闭

http://xqjay19910131-yahoo-cn.iteye.com/blog/1026321

Creating a Custom Dialog
(原文)

/**
如果需要一个自定义设计的dialog,你可以创建自己的layout。定义好layout后,传递root View对象或者leyout资源ID给setContentView(View)。
例如:
1、创建XML layout:
<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>
2、设置这个layout为这个dialog的内容    并定义ImageView和TextView的内容。
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后,使用setContentView(int)设置自定义的layout为dialog的content view。这时这个dialog已经定义好了layout,你可以使用dialog的findViewByID(int)来从layout中得到View对象,并修改其中的内容。
3、好了,现在你可以显示这个dialog了。

通过基类Dialog创建的dialog的必须有tittle,如果不调用setTittle(),那么tittle的空间会空着而且可见。如果你根本不需要tittle,那么应该使用AlertDialog来创建自定义的dialog。因为使用AlertDialog.Builder来创建AlertDialog更容易,你也不需要访问vsetContentView(int)而是用setView(View)。它接受一个View对象参数,所以你需要inflate the layout's root View object from
XML。
To inflate the XML layout,使用getLayoutInflater()或者getSystemService()得到LayoutInflater,然后调用inflate(int, ViewGroup)(第一个参数是layout的资源ID,第二个是root view的标识符)。至此,你可以用这个inflated layout来得到layout中的View对象并且定义其中的内容了。然后初始AlertDialog.Builder并setView为这个inflated layout。
例如:
AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER);
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();

Using an AlertDialog for your custom layout lets you take advantage of built-in AlertDialog features like managed buttons, selectable lists, a title, an icon and so on.
**/

/**
这篇文章也就这一块写的有点不知所以然..我附上代码先(这里简单的提供两种自定义的Dialog.Create Dialog,Create AlertDialog)

Java代码
复制代码
收藏代码
  1. //主要代码: 
  2. public void onClick(View arg0) { 
  3.         // TODO Auto-generated method stub 
  4.         switch (arg0.getId()) { 
  5.         case R.id.customid1: 
  6.             createCustomDialog1(); 
  7.             break
  8.         case R.id.customid2: 
  9.             createCustomDialog2(); 
  10.             break
  11.         default
  12.             break
  13.         } 
  14.     } 
  15.      
  16.     public void createCustomDialog1(){ 
  17.         Dialog dialog1 = new Dialog(context); 
  18.         dialog1.setContentView(R.layout.dialog_layout1); 
  19.         dialog1.setTitle("完善以下信息(Way 1):"); 
  20.         dialog1.show(); 
  21.     } 
  22.      
  23.     public void createCustomDialog2(){ 
  24.         AlertDialog dialog2 = null
  25.         AlertDialog.Builder builder = null
  26.         View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout1,null); 
  27.          
  28.         builder = new AlertDialog.Builder(context); 
  29.         builder.setTitle("完善以下信息(Way 2):"); 
  30.         builder.setView(view); 
  31.         dialog2 = builder.create(); 
  32.         dialog2.show(); 
  33.     } 

两种方案的效果:
The Way 1:

The Way 2:

Java代码
复制代码
收藏代码
  1. /**
  2. 现在分析原文的这句话:
  3. 3、好了,现在你可以显示这个dialog了。
  4. 通过基类Dialog创建的dialog的必须有tittle,如果不调用setTittle(),那么tittle的空间会空着而且可见。如果你根本不需要tittle,那么应该使用AlertDialog来创建自定义的dialog。因为使用AlertDialog.Builder来创建AlertDialog更容易,你也不需要访问vsetContentView(int)而是用setView(View)。它接受一个View对象参数,所以你需要inflate the
    layout's root View object from XML。
  5. To inflate the XML layout,使用getLayoutInflater()或者getSystemService()得到LayoutInflater,然后调用inflate(int, ViewGroup)(第一个参数是layout的资源ID,第二个是root view的标识符)。至此,你可以用这个inflated layout来得到layout中的View对象并且定义其中的内容了。然后初始AlertDialog.Builder并setView为这个inflated
    layout。**/
     

在以上提供的代码中分别去掉setTitle试试!
**/

抱歉!评论已关闭.