全局对话框透明对话框自定义对话框

为什么会写这玩意呢,对,是因为无聊,不过全局对话框在项目中还是经常用到的,主要是记得添加权限就好了,可能对刚入门的学弟学妹们可能有帮助;

权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
           <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

//简单的代码快

  1. public static void Dialog(Context context, String dStr) {
  2.         View view = View.inflate(context, R.layout.dialog_prompt, null);
  3.         AlertDialog.Builder b = new AlertDialog.Builder(context);
  4.         b.setView(view);
  5.         final AlertDialog d = b.create();
  6.         // 窗口可以获得焦点,响应操作
  7.         d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  8.         // 窗口不可以获得焦点,点击时响应窗口后面的界面点击事件
  9.         // d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
  10.         // 系统中关机对话框就是这个属性
  11.         // d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
  12.         d.setCancelable(false);// 按其他键不可退出
  13.         d.show();
  14.         TextView str = (TextView) view.findViewById(R.id.d_tv_str);
  15.         str.setText(dStr);
  16.         Button yesButton = (Button) view.findViewById(R.id.d_btn_ok);
  17.         yesButton.setOnClickListener(new View.OnClickListener() {
  18.             @Override
  19.             public void onClick(View v) {
  20.                 d.dismiss();
  21.             }
  22.         });
  23.         Window window = d.getWindow();
  24.         WindowManager.LayoutParams lp = window.getAttributes();
  25.         // 设置透明度为0.8,宽度为屏幕的一般
  26.         lp.alpha = 0.8f;
  27.         lp.width = width / 2;
  28.         window.setAttributes(lp);
  29.     }

//对了就是这么简单,xml还是根据自己的想要的效果来敲了,我这里为了筹字数就贴自己的上来了,大家自行忽略哈

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black_transparent_44"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="28sp" />

    <TextView
        android:id="@+id/d_tv_str"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="@color/white"
        android:textSize="22sp" />

    <View
        android:id="@+id/hotel_view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_margin="10dp"
        android:background="@color/blue_normal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/d_btn_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/hotle_btn_selector"
            android:paddingBottom="10dp"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:paddingTop="10dp"
            android:text="아  \t 니"
            android:textColor="@color/white"
            android:textSize="22sp"
            android:visibility="gone" />

        <Button
            android:id="@+id/d_btn_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/hotle_btn_selector"
            android:paddingBottom="10dp"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:paddingTop="10dp"
            android:text="확  \t 인"
            android:textColor="@color/white"
            android:textSize="22sp" />
    </LinearLayout>

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/qq_35350654/article/details/81165301