基于Android6.0实现弹出Window提示框
作者:WindFromFarEast 时间:2023-01-12 05:23:25
在项目中经常会需要应用弹出一些自定义的窗口,这时候Android自带的系统Dialog就无法满足我们的需求了,为了满足项目需求,我们可以使用Window来满足这一需求。
首先我们新建一个项目,来到MainActivity的布局文件,在这里新建一个按钮用于弹出Window
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="open window"/>
</LinearLayout>
接着我们完成window的布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingLeft="10dp"
android:background="@android:color/darker_gray"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="200dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="发件人——13110203356"
android:textSize="18sp"
android:textColor="@color/colorPrimary"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp"
android:text="这里是内容"
android:textSize="15sp"
android:textColor="@color/colorAccent"/>
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="按钮"
android:layout_gravity="right"/>
</LinearLayout>
</LinearLayout>
这个布局实际上类似于短信提示框,效果如下
之后在MainActivity中完成弹出Window的逻辑,首先我们要知道在Android6.0之前,使用Window只需要声明权限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>即可,但是从Android6.0开始,应用要想弹出一个悬浮在任意App上的Window的话需要用户手动为这个应用设置overlays权限,这个权限就连运行时权限也无法拿到,必须要用户前往手机的权限界面为应用设置该权限,因此在弹出window之前我们首先要进行一个逻辑判断,判断用户是否已经获取过overlays权限,如果获取了权限就直接弹出window,若没有获取过overlays权限则会自动将界面跳转到权限设置界面,让用户决定是否提供该权限,最后,代码如下
public class MainActivity extends AppCompatActivity
{
private Button btn_window;
private Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_window= (Button) findViewById(R.id.btn_window);
btn_window.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
{
//版本大于6.0则需要判断是否获取了overlays权限
if (!Settings.canDrawOverlays(MainActivity.this))
{
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:"+getPackageName())),1);
}
else
{
openWindow();
}
}
}
});
}
private void openWindow()
{
//获取WindowManager实例
final WindowManager windowManager= (WindowManager) getSystemService(Context.WINDOW_SERVICE);
//获取窗口布局参数实例
WindowManager.LayoutParams params=new WindowManager.LayoutParams();
//设置窗口布局参数属性
params.width= WindowManager.LayoutParams.MATCH_PARENT;
params.height=WindowManager.LayoutParams.MATCH_PARENT;
//设置window的显示特性
params.flags=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
//设置窗口半透明
params.format= PixelFormat.TRANSLUCENT;
//设置窗口类型
params.type=WindowManager.LayoutParams.TYPE_PHONE;
//获取窗口布局实例
final View windowView=View.inflate(this,R.layout.window,null);
//获取窗口布局中的按钮实例
btn_send= (Button) windowView.findViewById(R.id.btn_send);
//设置点击事件
btn_send.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
windowManager.removeView(windowView);
}
});
//显示窗口
windowManager.addView(windowView,params);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case 1:
{
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
{
if (Settings.canDrawOverlays(this))
{
//若用户开启了overlay权限,则打开window
openWindow();
}
else
{
Toast.makeText(this,"不开启overlay权限",Toast.LENGTH_SHORT).show();
}
}
break;
}
}
}
}
最后的效果如下,当然别忘了在Android6.0后也是需要在Manifest文件中声明权限的
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
来源:https://blog.csdn.net/xwx617/article/details/77619866
标签:Android,提示框
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法
2021-06-17 20:09:50
C#微信公众号开发之用户上下文WeixinContext和MessageContext
2022-04-23 09:31:54
为什么Java volatile++不是原子性的详解
2022-03-09 23:39:41
基于Spring-Security自定义登陆错误提示信息
2021-09-20 17:33:40
![](https://img.aspxhome.com/file/2023/2/70352_0s.png)
C#引用类型转换的常见方式总结
2022-03-02 16:53:58
c# 深拷贝与浅拷贝的区别分析及实例
2023-06-29 05:00:06
SpringBoot选择自有bean优先加载实现方法
2023-05-21 06:22:39
java 面试题闰年判断详解及实例
2023-11-27 19:09:01
SpringBoot与Angular2的集成示例
2021-09-02 22:24:55
SpringCloud Feign参数问题及解决方法
2022-08-08 08:00:35
基于Java文件输入输出流实现文件上传下载功能
2023-08-10 11:50:32
![](https://img.aspxhome.com/file/2023/0/58170_0s.png)
Apache SkyWalking 修复TTL timer 失效bug详解
2023-08-18 08:09:53
![](https://img.aspxhome.com/file/2023/5/58155_0s.png)
15个顶级Java多线程面试题(附答案)
2023-11-24 01:53:44
Java实现的并发任务处理实例
2023-05-22 17:51:10
ava实现一致性Hash算法
2022-09-18 00:44:33
![](https://img.aspxhome.com/file/2023/5/85825_0s.jpg)
解决SpringMVC拦截器path路径的坑
2023-03-24 17:02:01
Java 二分法检索算法代码实现详解
2022-01-05 19:13:24
![](https://img.aspxhome.com/file/2023/4/92544_0s.png)
关于C#委托三种调用的分享使用
2022-10-29 03:24:35
![](https://img.aspxhome.com/file/2023/5/80075_0s.png)
android 将图片压缩到指定的大小的示例
2021-07-29 23:09:23
Java多线程之线程同步
2023-01-24 18:39:00
![](https://img.aspxhome.com/file/2023/8/101988_0s.png)