基于Android6.0实现弹出Window提示框

 更新时间:2021年10月20日 10:44:15   作者:WindFromFarEast  
这篇文章主要为大家详细介绍了基于Android6.0实现弹出Window提示框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在项目中经常会需要应用弹出一些自定义的窗口,这时候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"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android this与Activity.this的区别

    Android this与Activity.this的区别

    这篇文章主要介绍了 Android this与Activity.this的区别的相关资料,需要的朋友可以参考下
    2016-09-09
  • Android快速开发之定制BaseTemplate

    Android快速开发之定制BaseTemplate

    这篇文章主要为大家详细介绍了Android快速开发之定制BaseTemplate的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android Scroll滑动效果实例

    Android Scroll滑动效果实例

    这篇文章主要为大家分享了Android Scroll滑动效果实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • Android实现全局悬浮框

    Android实现全局悬浮框

    这篇文章主要为大家详细介绍了Android实现全局悬浮框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • Android自定义View仿QQ运动步数效果

    Android自定义View仿QQ运动步数效果

    这篇文章主要为大家详细介绍了Android自定义View仿QQ运动步数效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • 深入理解Android中的xmlns:tools属性

    深入理解Android中的xmlns:tools属性

    关于xmlns:tools属性的介绍网上有很多,小编觉得有必要整理一篇介绍较为详细的内容给大家,下面这篇文章就很深入的介绍了关于Android中的xmlns:tools属性,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-12-12
  • Android 关于ExpandableListView去掉里头分割线的方法

    Android 关于ExpandableListView去掉里头分割线的方法

    下面小编就为大家带来一篇Android 关于ExpandableListView去掉里头分割线的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • Android使用ImageView实现支持手势缩放效果

    Android使用ImageView实现支持手势缩放效果

    这篇文章主要介绍了Android使用ImageView实现支持手势缩放效果,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • AndroidStudio kotlin配置详细介绍

    AndroidStudio kotlin配置详细介绍

    这篇文章主要介绍了AndroidStudio kotlin配置详细介绍的相关资料,需要的朋友可以参考下
    2017-05-05
  • Android序列化之Parcelable和Serializable的使用详解

    Android序列化之Parcelable和Serializable的使用详解

    本篇文章主要介绍了Android序列化之Parcelable和Serializable的使用详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01

最新评论