`

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

阅读更多

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试试!
**/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics