PopupWindow的基本使用

来源:互联网 发布:紫薇圣人 知乎 编辑:程序博客网 时间:2024/06/02 10:28

今天用popupwindow实现这样一个小效果


先了解下基本用法:

设置个Button,点击时,显示popupwindow,布局:

<Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="popup"        android:layout_centerInParent="true"        android:text="PopupWindow"/>
设置一个textview,展示文字

TextView tv = new TextView(getApplicationContext());        tv.setText("我是弹出窗口");        tv.setBackgroundColor(Color.RED);        //参数1,要显示的view;参数2,3,宽和高        PopupWindow popupWindow = new PopupWindow(tv, 100, 100);        //显示在某个View的左下角        popupWindow.showAsDropDown(mBtn);


也可以设置偏移量

 //显示在某个View的左下角,并设置偏移量        popupWindow.showAsDropDown(mBtn,50,50);


发现宽和高太大,那么文字的宽和高设置成包裹内容

//参数1,要显示的view;参数2,3,宽和高        PopupWindow popupWindow = new PopupWindow(tv, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);


但是我们发现,点击其他地方,popupwindow不消失,并且返回键退出时,报内存泄漏

MainActivity has leaked window android.widget.TextView
设置焦点和背景,实现点击其他地方和返回键,popupwindow消失,并且不会有内存泄漏问题

//参数1,要显示的view;参数2,3,宽和高        PopupWindow popupWindow = new PopupWindow(tv, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);        //设置popupwindow获得焦点        popupWindow.setFocusable(true);        //设置背景        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));        //显示在某个View的左下角,并设置偏移量        popupWindow.showAsDropDown(mBtn,50,50);

然后我们就可以实现开始的的那种效果了,代码如下

// 一个自定义的布局,作为显示的内容        View contentView = LayoutInflater.from(this).inflate(                R.layout.tips_popwindow, null);        //参数1,要显示的view;参数2,3,宽和高        PopupWindow popupWindow = new PopupWindow(contentView,                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);        //设置popupwindow获得焦点        popupWindow.setTouchable(true);        //设置背景        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));        //设置动画        popupWindow.setAnimationStyle(R.style.popwin_anim_style);        //设置居中位置和偏移量        popupWindow.showAtLocation(mBtn, Gravity.CENTER, 0, 0);
tips_popwindow的布局:
<LinearLayout        android:id="@+id/layout_show_tips"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:visibility="visible"        android:gravity="center_vertical|right"        android:orientation="horizontal">        <ImageButton            android:id="@+id/btn_show_tips"            android:layout_width="22.5dp"            android:layout_height="25dp"            android:background="@mipmap/tips_show"            android:scaleType="fitXY"/>        <TextView            android:id="@+id/tv_show_tips"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:textSize="14sp"            android:gravity="center_vertical"            android:paddingRight="3dp"            android:paddingLeft="3dp"            android:background="#ff7e2a"            android:text="我是温馨小提示"/>    </LinearLayout>
style中设置动画:

<style name="popwin_anim_style">        <item name="android:windowEnterAnimation">@anim/tips_show</item>        <item name="android:windowExitAnimation">@anim/tips_hide</item>    </style>
anim下的显示和隐藏动画:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromXDelta="100%"        android:toXDelta="0"        android:fromYDelta="0"        android:toYDelta="0"        android:duration="200" /></set>
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromXDelta="0%"        android:toXDelta="100%"        android:fromYDelta="0"        android:toYDelta="0"        android:duration="200" /></set>
实现了开始的效果






1 0
原创粉丝点击