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佈局充滿整個屏幕。

解決方法1:將佈局文件中的match_parent都變成wrap_content,或者對長寬加以限制。設置popupwindow的固定長寬。但是有時候寫佈局又很難避過。

解決辦法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);
}

最後的結果,額,忘記把按鈕也設置圓角了,希望能幫到大家: