Android PopupWindow使用总结

PopupWindow是Android里自定义的弹出窗口,但是在网上找了好久没有找到一个相关的、满意的博客,所以想自己写一个,分享给大家,也分享给自己。

构造函数

public PopupWindow(View contentView, int width, int height, boolean focusable)

这里比较重要的是最后一个boolean 类型的参数,focusable为设置是否可获得焦点
       如果为false,加载出的View只有在点击返回(或home键)的时候才会和Activity一起消失。此时编译器会报一个错误。所以我们一般都是设置为true。
       当然如果需要,Android还是贴心的提供了

public void setFocusable(boolean focusable)

来修改focusable的值。

自己的测试内容

    下边是自己写的一些PopupWindow的测试代码,和最后总结的函数功能,仅供参考。
PopupWindow的layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
>
    <Button
            android:id="@+id/btn_test"
            android:background="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:text="@string/app_name"
            android:textColor="#000000"
            android:gravity="center"
            android:onClick="onClick"
    />

</LinearLayout>

主界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
>
    <Button
            android:id="@+id/btn1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="设置点击空白区域(或back键)弹出窗口消失"
    />
    <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="设置点击空白区域弹出窗口不消失(点击back键弹出窗口消失)"
    />
    <Button
            android:id="@+id/btn3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="示例1"
            android:layout_gravity="center"
    />
    <Button
            android:id="@+id/btn4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="示例2"
            android:layout_gravity="center"
    />
</LinearLayout>

Activity

package com.imudges.AndroidStudy;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * Created by yangyang on 2016/12/10.
 */
public class PopupWindowTestActity extends Activity implements Button.OnClickListener{
    /**
     * for PopupWindow test
     * */
    PopupWindow mPopupWindow = null;
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popupwindow);
        initData();
        setButtonClick();
    }

    private void setButtonClick(){
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }

    private void initData(){
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        /**
         * .inflate(),查找layout/res 下的一个xml布局
         * */
        mPopupWindow = new PopupWindow(getLayoutInflater().inflate(R.layout.popupwindow_test,null), WRAP_CONTENT,WRAP_CONTENT,true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:{
                Log.v("1","点击了btn1");
                if(mPopupWindow !=null){
                    Log.v("!","进入btn1的if");
                    mPopupWindow.setTouchable(false);//控件可被点击
                    mPopupWindow.setOutsideTouchable(true);
                    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
                    mPopupWindow.showAsDropDown(view);
                }
                else{
                    Log.v("!","进入btn1的else");
                    mPopupWindow = new PopupWindow(getLayoutInflater().inflate(R.layout.popupwindow_test,null), MATCH_PARENT,MATCH_PARENT,true);
                    mPopupWindow.setTouchable(true);
                    mPopupWindow.setOutsideTouchable(true);
                    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
                    mPopupWindow.showAsDropDown(view);
                }
                break;
            }
            case R.id.btn2:{
                Log.v("2","点击了btn2");
                if(mPopupWindow !=null) {
                    Log.v("!","进入btn2的if");
                    mPopupWindow.setTouchable(false);//控件不可被点击
                    mPopupWindow.setOutsideTouchable(false);
                    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
                    mPopupWindow.update();
                    mPopupWindow.showAsDropDown(view);
                }
                else{
                    Log.v("!","进入btn2的else");
                    mPopupWindow = new PopupWindow(getLayoutInflater().inflate(R.layout.popupwindow_test,null), MATCH_PARENT,MATCH_PARENT,true);
                    mPopupWindow.setTouchable(false);
                    mPopupWindow.setOutsideTouchable(false);
                    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
                    mPopupWindow.showAsDropDown(view);
                }
                break;
            }
            case R.id.btn3:{
                Log.v("3","点击了btn3");
                if(mPopupWindow !=null) {
                    Log.v("!","进入btn3的if");
                    mPopupWindow.setTouchable(false);
                    mPopupWindow.setOutsideTouchable(true);
                    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
                    mPopupWindow.showAsDropDown(view);
                }
                else{
                    Log.v("!","进入btn3的else");
                    mPopupWindow = new PopupWindow(getLayoutInflater().inflate(R.layout.popupwindow_test,null), MATCH_PARENT,MATCH_PARENT,true);
                    mPopupWindow.setTouchable(false);
                    mPopupWindow.setOutsideTouchable(true);
                    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
                    mPopupWindow.showAsDropDown(view);
                }
                break;
            }
            case R.id.btn4:{
                Log.v("4","点击了btn4");
                if(mPopupWindow !=null) {
                    Log.v("!","进入btn4的if");
                    mPopupWindow.setFocusable(false);
                    /**
                     * 点击返回会直接结束Activity,程序报错,对于setTouchable和setOutsideTouchable,与之前总结的结果相同
                     * */
                    mPopupWindow.setTouchable(false);
                    mPopupWindow.setOutsideTouchable(false);
                    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
                    mPopupWindow.showAsDropDown(view);
                }
                else{
                    Log.v("!","进入btn4的else");
                    mPopupWindow = new PopupWindow(getLayoutInflater().inflate(R.layout.popupwindow_test,null), MATCH_PARENT,MATCH_PARENT,false);
                    mPopupWindow.setTouchable(false);
                    mPopupWindow.setOutsideTouchable(true);
                    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
                    mPopupWindow.showAsDropDown(view);
                }
                break;
            }
        }
    }
    public void _onClick(View view){
        Log.v("PopupWindow","Button被点击");
    }
}

总结

总结内容

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值