20161107
android中不依赖activity的dialog弹窗的实现
安卓里面,dialog的权限较低,所传的参数必须是一个窗口,所以叫做弹窗,但有时候你不确定用户会跳到哪个窗口,所以不好确定具体窗口,这个时候就得换个方式了。
private void showUpdateSuccessDialog(String s){
final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams para = new WindowManager.LayoutParams();
para.height = -2;//WRAP_CONTENT
para.width = -2;//WRAP_CONTENT
para.format = 1;
para.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
para.gravity = Gravity.CENTER;
para.type = WindowManager.LayoutParams.TYPE_TOAST;
final View contentView = LayoutInflater.from(context).inflate(R.layout.appdialog, null);//加载自定义布局
Button exit = (Button) contentView.findViewById(R.id.exit);//关闭弹窗
TextView tv = (TextView) contentView.findViewById(R.id.tv);//设置要显示的内容
tv_apad.setText(s);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(contentView);
}
});
wm.addView(contentView, para);
}
下面是布局
<?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="match_parent"
android:background="@color/white"//让背景变白,不然默认是透明
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/home_head_shape"//自定义的布局外围没有阴影效果,所以布局看着不明显,我给加了个框
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小提示"
android:layout_gravity="center_horizontal"
android:textSize="13sp"
android:layout_marginTop="20dp"
android:textColor="@color/blank"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="11sp"
android:layout_marginTop="20dp"
android:textColor="@color/blank"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<Button
android:id="@+id/exit"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="关闭"
android:textSize="13sp"
android:textColor="@color/white"
android:background="@color/red_e"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</LinearLayout>
下面是框的布局
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp" />
<solid android:color="#ffffff" />
<stroke
android:width="2dp"
android:color="#fd615d" />
</shape>
我是在Application里面应用的
借鉴博客:http://blog.csdn.net/u014583590/article/details/51899058

浙公网安备 33010602011771号