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

[size=large][b][color=green]
Creating a Custom Dialog[/color][/b][/size]
[color=red][b][size=large](原文)[/size][/b][/color]
[size=large][b][color=red]/**[/color][/b][/size]
如果需要一个自定义设计的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.
[size=large][b][color=red]**/[/color][/b][/size]

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


//主要代码:
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();
}


[b][color=green]两种方案的效果:[/color][/b]
The Way 1:
[img]http://dl.iteye.com/upload/picture/pic/89104/228a3f18-bfe9-357b-97d5-198ae6423032.gif[/img]

The Way 2:
[img]http://dl.iteye.com/upload/picture/pic/89106/43f57b73-e0c0-3ba2-8412-118f73baf3e8.gif[/img]


/**
现在分析原文的这句话:
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。**/

[color=red][b]在以上提供的代码中分别去掉setTitle试试![/b][/color]
[size=large][b][color=green]**/[/color][/b][/size]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值