为什么我的Android PopupWindow没有出现?

时间:2019-03-08 21:24:50

标签: java android android-layout

我一直在努力弄清为什么我的PopupWindow拒绝显示。我扩大了它的视图,并确保它出现在正确的视图容器中。

关于为什么窗口不显示的任何想法?

这是我在想要弹出窗口的活动中调用的代码:

    LinearLayout linearLayout = findViewById(R.id.webLinLayout);
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    ViewGroup container = (ViewGroup) inflater.inflate(R.layout.activity_popup_window, null);
    boolean focusable = false;
    PopupWindow popupWindow = new PopupWindow(container, 500, 500, focusable);
    popupWindow.showAtLocation(linearLayout, Gravity.CENTER, 0, 0);

我的活动布局的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".WebSearchActivity"
    android:background="@color/semitransparent"
    android:id="@+id/webLinLayout">

    <EditText
        android:id="@+id/imgSearchBar"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:maxLines="1"
        android:textAlignment="textStart"
        android:hint="Search for an image">
    </EditText>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="15dp"
        android:clickable="true">
    </android.support.v7.widget.CardView>

</LinearLayout>

这是我的弹出式布局:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/loadingMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="Fetching your images..."
        android:textSize="48sp"/>

    <android.support.v4.widget.ContentLoadingProgressBar
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/loadingMsg" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

尝试一下:

final LinearLayout linearLayout = findViewById(R.id.webLinLayout);

LayoutInflater inflater =
        (LayoutInflater)
                getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);

final ViewGroup container =
        (ViewGroup) inflater.inflate(R.layout.activity_popup_window, null);

boolean focusable = false;

final PopupWindow popupWindow = new PopupWindow(container, 500, 500, focusable);
linearLayout.post(new Runnable() {
    public void run() {
        popupWindow.showAtLocation(linearLayout, Gravity.CENTER, 0, 0);
    }
});