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)
//主要代码:
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.customid1:
createCustomDialog1();
break;
case R.id.customid2:
createCustomDialog2();
break;
default:
break;
}
}
public void createCustomDialog1(){
Dialog dialog1 = new Dialog(context);
dialog1.setContentView(R.layout.dialog_layout1);
dialog1.setTitle("完善以下信息(Way 1):");
dialog1.show();
}
public void createCustomDialog2(){
AlertDialog dialog2 = null;
AlertDialog.Builder builder = null;
View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout1, null);
builder = new AlertDialog.Builder(context);
builder.setTitle("完善以下信息(Way 2):");
builder.setView(view);
dialog2 = builder.create();
dialog2.show();
}
两种方案的效果:
The Way 1:
The Way 2:
/**
现在分析原文的这句话:
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。**/
在以上提供的代码中分别去掉setTitle试试!
**/
分享到:
相关推荐
通过一个简单的例子实现了Android系统对话框和自定义对话框,有需要的可以看看!
自定义对话框自定义对话框自定义对话框自定义对话框自定义对话框
Dialog对话框之android 自定义对话框代码 设置对话框显示一个单选的List 用来设置对话框显示一系列的复选框 设置进度条风格,风格为圆形,旋转的
Android例子源码自定义对话框
这个示例演示对话框的用法,包括普通的AlertDialog,列表对话框,进度对话框,自定义对话框 这里实现一个在3秒后自动关闭对话框的功能,通过一个定时器来完成
安卓Android源码——android 自定义对话框.rar
050集-列表对话框和自定义对话框视频教程,很是不错哦,有兴趣的可以学习下。
完全自定义Android对话框AlertDialog的实现,在Android2.1下编译通过
Android应用源码之android 自定义对话框.zip
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...
android 自定义单选、多选对话框及popwindow窗口实例源码.zip
一般自定义对话框有三种办法: 1、重写Dialog来实现。 2、获取Dialog的Window对象实现。 3、使用WindowManager来实现。 4、使用DialogTheme来实现。 详细请看:...
android自定义对话框、dianlog;详细讲解请参考:http://blog.csdn.net/tangron/article/details/19474385
本文实例讲述了Android编程实现在自定义对话框中获取EditText中数据的方法。分享给大家供大家参考,具体如下: 在项目中忽然遇到这样的问题,需要自定义对话框,对话框需要有一个输入框,以便修改所选中的价格,然后...
Android自定义对话框Dialog(界面美化)
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...
这是一个Android自定义加载对话框,实现显示和取消对话框功能,简单的小功能希望对你有用.
android 很不错的自定义对话框+返回 菜单 键监听 源码里有运行图效果。 按返回键时,弹出自定义的对话框供用户选择 Yes/NO 按菜单键时,演示等待对话框,常用于等待某一事件时,动态显示.
android 自定义对话框,android模仿百度地图自定义对话框
绝对给力,你想搞成什么颜色都可以的哦,在xml里面设置,好high的对话框,完全自定义Android对话框AlertDialog的实现,编译通过,代码完整