效果图:
1.文字+按钮对话框:
(1)编写对话框背景形状(建立已个4个圆角,背景透明的形状),名称为roundrect
<?xml version="1.0"encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="1dp"android:bottom="1dp"android:left="1dp"android:right="1dp">
<shape>
<solid android:color="#FFFFFF"/>
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"
android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/>
</shape>
</item>
</layer-list>
(2)创建对话框layout,名称为dialog_alarm
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dip"
android:layout_gravity="center"
android:background="@drawable/roundrect"//引用对话框形状
android:id="@+id/df">
<RelativeLayoutandroid:layout_width="@dimen/dialog_width"
android:layout_height="match_parent"
android:background="@drawable/shape_top_corner"
>
<TextViewandroid:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@color/white"
android:layout_marginLeft="10dip"
android:text="@string/str_alert"
android:textSize="30dip"
android:id="@+id/tvTitle"/>
</RelativeLayout>
<RelativeLayoutandroid:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip">
<TextViewandroid:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@color/gray"
android:layout_marginLeft="10dip"
android:text="@string/str_alert_message"
android:textSize="15dip"
android:id="@+id/tvTitl2e"/>
</RelativeLayout>
<LinearLayoutandroid:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:orientation="horizontal"
>
<Button
android:background="@drawable/button_click"
android:textColor="@color/white"
android:layout_margin="10dip"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/str_certain"
android:id="@+id/btCertain"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_weight="1"
/>
<Button
android:background="@drawable/button_click"
android:textColor="@color/white"
android:layout_margin="10dip"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/str_cancel"
android:id="@+id/btCancel"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btCertain"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
(3)添加自定义对话框类
public class CustomDialog extends Dialog {
public CustomDialog(Context context,View layout, int style) {
super(context, style);
setContentView(layout);
Window window = getWindow();
WindowManager.LayoutParams params= window.getAttributes();
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
}
(4)在style中添加对话框样式
<style name="DialogStyle"parent="@android:style/Theme.Dialog">
<itemname="android:windowNoTitle">true</item> <!--对话框无title-->
<item name="android:windowBackground">@android:color/transparent</item><!--自定义布局以外的部分变透明-->
<itemname="android:backgroundDimEnabled">true</item> <!--背景是否变暗-->
<itemname="android:backgroundDimAmount">0.6</item>
</style>
(5)在程序中创建自定义对话框
//函数:creatRestoreDialog
//功能:创建恢复出厂设置对话框
private void creatRestoreDialog() {
LayoutInflater inflater =LayoutInflater.from(SettingsActivity.this);
View View =inflater.inflate(R.layout.dialog_alarm, null);//引用对话框layout文件
restoreDialog = newCustomDialog(SettingsActivity.this, View, R.style.DialogStyle);
restoreDialog.show();
Button btCertain = (Button)View.findViewById(R.id.btCertain);
Button btCancel = (Button)View.findViewById(R.id.btCancel);
btCancel.setOnClickListener(onRestoreCancelClick);//添加按钮点击事件
btCertain.setOnClickListener(onRestoreCertainClick);
}
2.其他对话对话框具体看事例代码步骤相同
工程地址: