我最终没有使用 AlertDialog,而是使用了 Dialog。要获得自定义外观:
1-创建对话框并删除标题区域(否则你会在顶部得到一个空白的灰色区域):
myDialog = new Dialog(this);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
2-在xml中设计一个布局,并设置为对话框的内容:
myDialog.setContentView(R.layout.mydialog_layout);
3-如果布局不是圆角矩形,它将与对话框的圆角相交。因此,将布局设计为圆角矩形:
在 mydialog_layout.xml 中:
android:background = "@layout/mydialog_shape"
mydialog_shape.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient android:startColor="#FF0E2E57"
android:endColor="#FF0E2E57"
android:angle="225" android:paddingLeft="20dip"/>
<corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp" android:topRightRadius="5dp" android:paddingLeft="20dip"/>
</shape>
4-将侦听器添加到活动中的按钮:
Button button = (Button)myDialog.findViewById(R.id.dialogcancelbutton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myDialog.cancel();
}});
就是这样。