PopupWindow设置宽高wrap_content无效

之前一直都是pop的布局,直接用一个match_parent的半透明的蒙版,然后中间再显示,这个大概也算一种解决wrap_content无效的方法,但是这个假如加上pop的进出动画的话,会带着蒙版一起进出,不太好看,也可以背景全透明,然后设置窗口的setAttributs,但感觉是掩耳盗铃。设置宽高wrap_content无效的问题,是部分的,比较讲缘分,所以搜到的讯息不多。

我碰到的问题是:在布局文件中设置了固定宽高:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="270dp"
    android:layout_gravity="center"
    android:background="@drawable/round_rect15_white"
    android:layout_height="136dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提示"
        android:textColor="@color/black_03"
        android:textSize="17sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"/>

    <TextView
        android:id="@+id/tv_explain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13sp"
        android:textColor="@color/black_03"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="53dp"/>
    
    <LinearLayout
        android:id="@+id/ll_bt"
        android:layout_width="match_parent"
        android:layout_height="43dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/bt_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/black_55"
            android:textSize="17sp"
            android:background="@color/white"
            android:text="@string/cancel"/>
        <Button
            android:id="@+id/bt_next"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#007AFF"
            android:textSize="17sp"
            android:background="@color/white"
            android:text="@string/ok"/>
        
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_above="@+id/ll_bt"
        android:background="#784D4D4D"/>

</RelativeLayout>

然后再在自定义pop里面

LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = inflater.inflate(R.layout.delete_popup_window, null);
setContentView(mView);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

这应该是网上大部分教程的写法,感觉是很合情合理,我设置了宽高,然后让popwupwindow自适应我的布局文件,进行显示,但是结果是:

额。图片有点大。设置显示方式是showAtLocation。但是结果却是,看着是match_parent。

我的理解是,popupwindow设置的宽高,把layout的viewgroup的宽高给顶掉了,当layout的外层变成wrap_content,里面设置的match_parent,就会使得layout布局充满整个屏幕(当时理解错误)layout_width和layout_height都是相对于父布局的宽度和高度,但是popupwindows没有父布局,自然设置的宽高也就无效了。

解决方法1:将布局文件中的match_parent都变成wrap_content,或者对长宽加以限制。设置popupwindow的固定长宽。但是有时候写布局又很难避过。(PS.java代码中设置的宽高是像素,不是dp,需要用dp转px工具转化一下)

解决办法2:既然是把viewGroup给顶掉了。没有父布局,那加一个viewGroup就行了,可以在布局中加,也可以在设置popupWindow中

LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = new LinearLayout(context);
View mView = inflater.inflate(R.layout.delete_popup_window, linearLayout);

在将布局文件infate的时候,给它加一个viewGroup,再把这个view设置给pop,这个LayoutInflater.inflater第二个参数本来就是ViewGroup,只是一般把他写成了null

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, root != null);
}

最后的结果,额,忘记把按钮也设置圆角了,希望能帮到大家:

猜你喜欢

转载自blog.csdn.net/qq_27454233/article/details/80614354