Xamarin.Android页面实现悬浮框弹出

Xamarin Android实现Activity页面悬浮框弹出,底层半透明遮挡效果。

1.创建要弹出的Activity页面视图:IndexFloatView;

2.主体核心代码如下:

        #region 全局变量定义

        ///


        /// 定义浮动窗口布局对象
        ///

        LinearLayout mFloatLayout;

        ///
        /// 悬浮框Layout对象参数
        ///

        WindowManagerLayoutParams wmParams;

        ///
        /// 创建浮动窗口设置布局参数的对象
        ///

        IWindowManager mWindowManager;

        ///
        /// 悬浮框关闭按钮对象
        ///

        ImageButton mFloatView;
        #endregion

#region 创建悬浮框
        ///


        /// 创建悬浮框
        /// Author:William(徐威)
        /// Create Time:2018-05-05
        ///

        private void createFloatView()
        {
            wmParams = new WindowManagerLayoutParams();
            //创建WindowManager接口实现对象
            mWindowManager = this.GetSystemService(Context.WindowService).JavaCast();
            //设置窗体类型
            wmParams.Type = WindowManagerTypes.Phone;
            //设置图片格式,效果为背景透明  
            wmParams.Format = Format.Translucent;
            //设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)  
            wmParams.Flags = WindowManagerFlags.NotFocusable;
            //调整悬浮窗显示的停靠位置为左侧置顶  
            wmParams.Gravity = GravityFlags.Center;
            // 以屏幕左上角为原点,设置x、y初始值,相对于gravity  
            wmParams.X = 0;
            wmParams.Y = 0;

            //设置悬浮窗口长宽数据  
            //不设置则全屏
            //wmParams.Width = 1350;
            //wmParams.Height = 800;
            
            LayoutInflater inflater = LayoutInflater.From(this.ApplicationContext);   //LayoutInflater.from(getApplication());
            //获取浮动窗口视图所在布局  
            mFloatLayout = (LinearLayout)inflater.Inflate(Resource.Layout.IndexFloatView, null);
            //添加mFloatLayout  
            mWindowManager.AddView(mFloatLayout, wmParams);
            //浮动窗口按钮  
            mFloatView = mFloatLayout.FindViewById< ImageButton >(Resource.Id.imgBtnCloseIndexFloatWindow);
            mFloatView.SetOnClickListener(this);

            mFloatLayout.Measure(View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified),
                View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified));

        }
        #endregion

        #region 移除Android悬浮框
        ///
        /// 移除Android悬浮框
        /// Author:William(徐威)
        /// Create Time:2018-05-05
        ///

        private void CloseFloatWindow()
        {
            if (mFloatLayout != null)
            {
                //移除悬浮窗口  
                mWindowManager.RemoveView(mFloatLayout);
            }
        }
        #endregion

具体如何操作,大家自己组装,也可以访问下面我的资源上传地址下载地址:https://download.csdn.net/download/xuwei_net/10394244。

效果图如下图片。

你可能感兴趣的:(Xamarin.Android,设置Activity,背景色透明)