美文网首页
popupWindow使用常见的错误

popupWindow使用常见的错误

作者: 不会看源码 | 来源:发表于2018-01-16 14:50 被阅读0次

这个是在使用popupWindow时候出现常见的问题,如布局设置大小不成功,点击popup区域外面无法正常关闭popupWindow等。

1.设置主布局大小无效(layout_width, layout_height)
例子:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="@color/color_white"
    android:gravity="center"
    >
    <!--当你在popupWindow直接使用这个布局的时候,-->
    <!--会发现200 x 100没有效果-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="123"
        />
</LinearLayout>

解决方法:
方法一:多加一个父布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/color_white"

    >
    <!--多套一个父布局,则设置大小成功-->
    <!--会发现200 x 100成功-->
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:gravity="center"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="123"
            />
    </LinearLayout>
</LinearLayout>

方法二:使用window测量,控制大小

Window w = getWindow();
        if (w != null) {
        WindowManager.LayoutParams lp = w.getAttributes();
        w.setGravity(Gravity.CENTER);
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (manager != null) {
            //设置popupWindow的布局
            setContentView(mInflate);
            manager
                .getDefaultDisplay()
                .getMetrics(metrics);
            int width = metrics.widthPixels;
            int height = metrics.heightPixels;
            //这里是设置popupWindow的宽度为手机屏幕的80%
            lp.width = (int) (width * 0.8);
            lp.height = LinearLayout.LayoutParams.WRAP_CONTENT | (int) (height * 0.8);
            w.setAttributes(lp);
            initView(mInflate);
            
        }
    }

关于测量宽高无效的问题,宽高一直都是0

//有时候,你会发现获取的宽高一直都是0
int height = mPopup.getHeight();
int width = mPopup.getWidth();

解决:

//https://www.cnblogs.com/gumf/p/4148863.html
//强制绘制view的大小,否则getHeight , getWith都是0
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
//mPopupView就是自定义popupWindow的布局
mPopupView.measure(widthMeasureSpec,heightMeasureSpec);

2.设置点击外部消失无效

mPopupWindow.setOutsideTouchable(outsideTouchable);

解决一:设置透明背景

//设置透明
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
//设置点击外部消失
mPopupWindow.setOutsideTouchable(outsideTouchable);
mPopupWindow.setFocusable(true);

解决二:查看你自定义的popupWindow的布局,有时候可能是布局的问题,尽量为精简

【遇到的问题】

//使用这个显示的时候,出现点击外部消失popup问下异常
mPopup.showAtLocation(mAll, Gravity.BOTTOM, 0, 0);

解决:就是因为我在自定义布局的时候,多套用了一个父布局,去掉则可以。

3.关于获取焦点的问题

mPopupWindow.setFocusable(focusable)

如果设置为false,在按返回键的时候直接退出布局,
它是可以点击activity上控件的点击事件。

如果设置为true,在按返回键的时候,popupWindow显示则关闭,
并且popupWindow上存在Edittext,必须需要设置true,否则无法输入。
activity上控件的点击事件无法点击。

那么问题来了,当你既想popupWindow上的Edittext可以使用,又想activity上控件的点击事件可以使用。
找到了解决方法,但是我没有测试过:
http://www.cnblogs.com/mengdd/p/3569127.html

相关文章

网友评论

      本文标题:popupWindow使用常见的错误

      本文链接:https://www.haomeiwen.com/subject/ebusoxtx.html