AlertDialog自己定義View的使用方法+怎樣改變彈出框的大小
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);
AlertDialog自己定義View的使用方法+怎樣改變彈出框的大小