Android popupWindow 点击外部消失,点击外部其他控件触发其他事件 PopupWindow设置背景

Android popupWindow 点击外部消失,点击外部其他控件触发其他事件

  1. // 需要设置一下此参数,点击外边可消失  
  2.  pop.setBackgroundDrawable(new BitmapDrawable());  
  3. // 设置点击窗口外边窗口消失  
  4. pop.setOutsideTouchable(true);  
  5. // 设置此参数获得焦点,否则无法点击  
  6. pop.setFocusable(true);  

PopupWindow设置背景

一些常用的方法设置PopupWindow的属性

这里写图片描述

  • 效果图如上,怎么给PopupWindow加背景?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"  android:layout_width="match_parent"
    android:background="#66000000"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <GridView
        android:id="@+id/gridview"
        android:numColumns="6"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

代码设置

mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); //设置宽度 布局文件里设置的没有用
        mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);  //设置高度  必须代码设置
  • 1
  • 2

在最外层布局 设置上你要的背景android:background=”#66000000”属性,

  • 怎么设置动画
 //设置动画
        mPopupWindow.setAnimationStyle(R.style.AnimTheme);

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AnimTheme">
        <item name="android:windowEnterAnimation">@anim/show</item>
        <item name="android:windowExitAnimation">@anim/hide</item>
    </style>

</resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

show.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0"
        android:toYDelta="0"
        android:toXDelta="0"
        android:fromYDelta="100%"
        android:duration="2000"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        />
</set>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

hide.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AnimTheme">
        <item name="android:windowEnterAnimation">@anim/show</item>
        <item name="android:windowExitAnimation">@anim/hide</item>
    </style>

</resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

其他的一些属性设置方法

  //4者的属性必须设置
        mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); //设置宽度 布局文件里设置的没有用
        mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);  //设置高度  必须代码设置
        mPopupWindow.setContentView(window_view);  //设置布局
        //popupWindow.showAsDropDown(window_view);  //设置显示位置

        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setOutsideTouchable(true); //外部是否可点击 点击外部消失
        mPopupWindow.setFocusable(true);  //响应返回键  点击返回键 消失

        View parentView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        mPopupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);      //相对父布局
        // mPopupWindow.showAsDropDown(mBt, 0, 0);   //相对某个控件显示

猜你喜欢

转载自blog.csdn.net/weixin_40430041/article/details/79052913