自定义dialog的建立
如果你想自定义dialog的布局,你可以自己创建一个dialog布局。定义好之后,传递根View对象或者资源ID到setContextView(View)方法。
例如,如上图的dialog:
1-建立一个xml布局文件custom_dialog.xml;
view plaincopy to clipboardprint?
<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>
<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>
这个xml在LinearLayout里定义了一个ImageView和TextView。
2-设置上面的布局为dialog的context view ,并且定义ImageView和TextView两个元素。
view plaincopy to clipboardprint?
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);
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后,使用setContextView(int)方法设置自定义的布局。现在dialog便有了一个自定义的布局,你可以使用findViewById(int)方法来获得或者修改布局。
3-完成了,现在你可以显示自定义的dialog了。
一个dialog必须有一个title,如果你没有调用setTitile()方法,那么会标题处会显示空,但dialog仍然可见,如果你不想显示标题,只有写一个自己的dialog类了。然而,因为一个AlertDialog使用AlertDialog.builder类创建起来非常简单,你不必使用setContextView(int)方法。但必须使用setView(view)方法代替。这个方法会接受一个view参数,你需要从xml中得到根view元素。
得到xml布局,通过LayoutInflater类的getLayoutflater()方法(或者getSystemService()方法),然后调用inflate(int,ViewGroup)方法,第一个参数是xml文件id,第二个参数是根view的id,在这点上,你可以使用inflated 布局来获得xml中的view对象并且定义ImageView和TextView对象,然后实例化AlertDialog.Builder类并且使用setView(View)方法来设置布局。
这有一个自定义dialog布局文件的例子:
view plaincopy to clipboardprint?
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
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();
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
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();
使用自定义布局这种方式来生成dialog,可以让你使用更高级的特性,比如管理按钮、列表、标题、图标等。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/iamlazybone/archive/2010/10/02/5919094.aspx