android系统定义了弹出框,支持我们自己定义布局:
public AlertDialog getEditCustomDialog() { LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.custom_message_rename, null); AlertDialog.Builder builder = new AlertDialog.Builder(AnimationTest.this); builder.setView(view); builder.setTitle("A New Version is Available"); return builder.create(); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/rc_document_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:imeOptions="flagNoEnterAction" android:inputType="textNoSuggestions" android:maxLines="5" android:textColor="#000000" android:textSize="@dimen/font_size_medium" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="10dp" android:background="?
android:attr/dividerHorizontal" /> <LinearLayout android:id="@+id/LinearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:measureWithLargestChild="true" android:orientation="horizontal" android:padding="0dp" > <Button android:id="@+id/cancelBtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="?android:attr/selectableItemBackground" android:text="Cancel" /> <View android:id="@+id/postCancelBtnDivider" android:layout_width="1dp" android:layout_height="match_parent" android:background="?android:attr/dividerHorizontal" /> <Button android:id="@+id/okBtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="?
android:attr/selectableItemBackground" android:text="OK" /> </LinearLayout> </LinearLayout>
效果图是:
另一种常见的样式是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/title" android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_marginTop="20dp" android:layout_height="60dp" android:layout_marginLeft="25dp" android:text="@string/upgrade_content" android:textColor="#000000" android:textSize="22sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@id/content" android:orientation="horizontal" > <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="15dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="15dp" android:text="@string/remind_me" android:textColor="#000000" android:textSize="14sp" /> </LinearLayout> </RelativeLayout>
效果图是:
假设想改变Dialog的大小能够这样写:
<span style="white-space:pre"> </span>AlertDialog dialog = getCustomDialog(); dialog.show(); //一定得在show完dialog后来set属性 WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); lp.width = AnimationTest.this.getResources().getDimensionPixelSize(R.dimen.dialog_width); lp.height = AnimationTest.this.getResources().getDimensionPixelSize(R.dimen.dialog_height); dialog.getWindow().setAttributes(lp);