Android学习——PopupWindow

1.弹出 showAtLocation
2.销毁:dismiss()
3.设置退出:
popupWindow_view.setOnKeyListener
popupWindow_view.setOnTouchListener

1. 弹出与销毁
示例:
activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="popup"
        android:text="弹出" />

</RelativeLayout>

新建布局文件res/layout/popup.cml作为弹出窗口的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#44000000">

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/ratingBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/ratingBar3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

java代码:

public class MainActivity extends Activity {
    private PopupWindow pop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View contentView = View.inflate(this, R.layout.popup, null);
        //初始化一个PopupWindow,指定窗口大小参数。
        //true 表示PopupWindow拿到焦点,目的是当点击back键时不至于整个应用程序退出
        //false,点击back键时,直接退出程序
        pop = new PopupWindow(contentView,100,200,true);
        //设置可以获得焦点,必须有焦点才能触发监听事件
        contentView.setFocusable(true);
        contentView.setFocusableInTouchMode(true);
        //方式一:当按back返回键时pop退出
        contentView.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    // 按back返回键,销毁PopupWindow
                    pop.dismiss();
                }
                return false;
            }
        });     
    }
    public void popup(View v) {
        //弹出PopupWindow
        pop.showAtLocation((View) v.getParent(), Gravity.CENTER, 0, 0);
        //contentView获取焦点
        v.requestFocus();
    }
}

运行效果:
这里写图片描述
点弹出按钮后效果:
这里写图片描述
按back返回键,PopupWindow消失。
也可以给contentView添加触摸监听:

        //方式二:当按到contentView的时候pop退出,不常用
        contentView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                pop.dismiss();
                return false;
            }
        });

注意:焦点的获取,默认的PopupWindow不响应back键退出,程序会卡死。

区别:PopupWindow与Dialog
PopupWindow:线程阻塞的,由主线程弹出,占用UI资源,窗口可以设为透明
Dialog:子线程负责弹出,线程不阻塞,但是窗口不能设为透明的。
2. AnimationStyle——设置进出场动画
(1)新建res/anim/in.xml 和res/anim/out.xml动画文件:

in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="3000"
        android:fromAlpha="0"
        android:toAlpha="1" />

    <translate
        android:duration="3000"
        android:fromXDelta="-100%"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="0" />

</set>
out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="3000"
        android:fromAlpha="1"
        android:toAlpha="0" />

    <translate
        android:duration="3000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="-100%"
        android:toYDelta="0" />

</set>

(2)新建res/values/animstyle.xml动画样式文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <!-- PopupWindow左右弹出的效果 -->
    <style name="AnimationFade">     
        <!-- PopupWindow进入效果 -->
        <item name="android:windowEnterAnimation">@anim/in</item>
        <!-- PopupWindow退出效果 -->
        <item name="android:windowExitAnimation">@anim/out</item>
    </style>
</resources>

(3)在onCreate方法中为PopupWindow设置动画样式:
pop.setAnimationStyle(R.style.AnimationFade);//设置动画样式
运行效果:
进入时,从左到右,透明度从0到1
退出时,从右到左,透明度从1到0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值