一些常用的方法设置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); //设置高度 必须代码设置
在最外层布局 设置上你要的背景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>
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>
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>
其他的一些属性设置方法
//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); //相对某个控件显示
暂时整理这么多,后期会更新加入新的知识点。