您的位置:首页 > 其它

自定义一个退出AlertDialog(对话框)

2015-11-25 21:18 393 查看
遭遇揪心问题:

1、对话框难看,预览与实际运行结果严重不符。

2、对话框有黑角。

可能形成的原因:

1、对话框中的控件使用了包裹内容,于是 android 任性地自适应了,所以导致预览与结果相差较大,解决的办法是写死控件的大小。

2、对话框的xml根布局标签中,设置了背景,导致背景色被设为对话框的背景,由此显示出来。

关于AlertDialog:

1、弹出对话框后,屏幕自动变暗,此时整个屏幕存在一张全屏半透明的蒙版,遮挡了Activity的内容。

2、然后,对话框将xml布局的根标签的背景色添加到对话框的底层,大小为对话框大小,且为一个矩形。不设置时此层透明。

3、然后,将xml布局的子布局添到第2步矩形层的上方,此时,若此子layout的 background 没有充满该矩形,矩形就会显露出来。因此,可能导致黑角,黑背景的诞生,这取决于xml根标签的android background = “”属性。如果设为#00000000,即表示全透明,与不设置等同。前两位表示透明度,后6位是颜色。

问题图:



问题产生的原因如上所诉,仅仅是因为此对话框的xml文件多出了android:background=”#005566”用来设置背景:

dialog_exit.xml

[code]<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#005566"
    >
    ……
    </RelativeLayout>


[code]去掉此行代码即可。或者设为android:background="#00000000"


解决问题后,上效果图:



所有代码:

MyAlerDialog.java

[code]
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * 自定义的对话框
 */
public class MyAlerDialog extends AlertDialog implements
        android.view.View.OnClickListener {
    private Button btn_ok,btn_cancel;//确定,取消
    private Context context;

    public MyAlerDialog(Context context) {
        super(context);
        this.context = context;

    }

    /**
     * 布局中的其中一个组件
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 加载自定义的内容视图
        setContentView(R.layout.dialog_exit);
        btn_ok = (Button) findViewById(R.id.btn_ok_dialog);
        btn_cancel = (Button)findViewById(R.id.btn_cancel_dialog);
        btn_ok.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
        }

    @Override
    public void onClick(View v) {
        if (v==btn_cancel) {
            dismiss();//取消对话框
        }
        if(v==btn_ok){//关闭当前进程,退出程序
            dismiss();
            int pid = android.os.Process.myPid();
            android.os.Process.killProcess(pid);   //杀死当前进程...相当暴力
        }
    }
    }


MainActivity.java

[code]
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;

public class MainActivity extends Activity {

    //自定义的弹出框类
    RoundSelectPopupWindow menuWindow;

    /* (non-Javadoc)
     * @see android.app.AlertDialog#onKeyDown(int, android.view.KeyEvent)
     * 可以监视键盘,返回键,home键
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode==KeyEvent.KEYCODE_BACK) {
              MyAlerDialog mydialog = new MyAlerDialog(MainActivity.this); 
                mydialog.show();  
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        }
        }


layout包下:

dialog_exit.xml

[code]<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:background="@drawable/dialog_background" >

        <TextView
            android:id="@+id/tv_text_in_top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp"
            android:textSize="20sp"
            android:text="退出" />

        <TextView
            android:id="@+id/tv_text_in_center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="主人,真的要离开我么?" />

        <LinearLayout
            android:id="@+id/layout_ok_cancel_dialog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            >

            <Button
                android:id="@+id/btn_ok_dialog"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/button_ok_cancel"
                android:text="确定" />

            <Button
                android:id="@+id/btn_cancel_dialog"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/button_ok_cancel"
                android:text="取消" />

        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>


drawable 包下,没有则创建:

对话框的背景:

dialog_background.xml

[code]<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <solid 
        android:color="#ff66aa"/>
    <corners 
         android:radius="50dp"/>
</shape>


按钮的背景:

background_ok_cancel.xml

[code]<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <!-- 填充暗橙*** -->
    <solid 
        android:color="#ff8c00"/>
    <!--
    圆角 
    <corners 
        android:radius="5dp"/>
     -->
</shape>


ok,完成。运行模拟器,点击返回键即弹出退出对话框。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: