为什么会写这玩意呢,对,是因为无聊,不过全局对话框在项目中还是经常用到的,主要是记得添加权限就好了,可能对刚入门的学弟学妹们可能有帮助;
权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
//简单的代码快
- public static void Dialog(Context context, String dStr) {
- View view = View.inflate(context, R.layout.dialog_prompt, null);
- AlertDialog.Builder b = new AlertDialog.Builder(context);
- b.setView(view);
- final AlertDialog d = b.create();
- // 窗口可以获得焦点,响应操作
- d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
- // 窗口不可以获得焦点,点击时响应窗口后面的界面点击事件
- // d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
- // 系统中关机对话框就是这个属性
- // d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
- d.setCancelable(false);// 按其他键不可退出
- d.show();
- TextView str = (TextView) view.findViewById(R.id.d_tv_str);
- str.setText(dStr);
- Button yesButton = (Button) view.findViewById(R.id.d_btn_ok);
- yesButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- d.dismiss();
- }
- });
- Window window = d.getWindow();
- WindowManager.LayoutParams lp = window.getAttributes();
- // 设置透明度为0.8,宽度为屏幕的一般
- lp.alpha = 0.8f;
- lp.width = width / 2;
- window.setAttributes(lp);
- }
//对了就是这么简单,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>