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. public void createCustomDialog1(){
  16. Dialog dialog1 = new Dialog(context);
  17. dialog1.setContentView(R.layout.dialog_layout1);
  18. dialog1.setTitle("完善以下信息(Way 1):");
  19. dialog1.show();
  20. }
  21. public void createCustomDialog2(){
  22. AlertDialog dialog2 = null;
  23. AlertDialog.Builder builder = null;
  24. View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout1,null);
  25. builder = new AlertDialog.Builder(context);
  26. builder.setTitle("完善以下信息(Way 2):");
  27. builder.setView(view);
  28. dialog2 = builder.create();
  29. dialog2.show();
  30. }


两种方案的效果:
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试试!
**/

更多相关文章

  1. Android8.0源码解析——Activity的启动过程
  2. Android(安卓)内存溢出问题分析
  3. 调用Android系统设置
  4. 活用Android的Message Queue(1)
  5. BLE 扫描及连接 android程序开发(一)
  6. Android(安卓)组件系列-----Activity的传值和回传值
  7. Android多线程之HandlerThread
  8. Android内存泄漏检测工具使用手册
  9. Android与Js调用

随机推荐

  1. Android基本框架笔记
  2. Android 开发简介-系统架构
  3. android使用文件进行存储
  4. Android 面试题总结之Android 进阶(二)
  5. Android当中的MVP模式(三)基于分页列表的封
  6. Android创始人安迪 罗宾(Andy Rubin)离职
  7. Android(安卓): 自己写个HTTP框架
  8. android textView 技巧 富文本显示 代码
  9. Android(安卓)butterknife注入完毕后不起
  10. Android(安卓)布局之GridLayout