简体   繁体   English

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

[英]Why doesn't my Android PopupWindow appear?

I have been struggling to figure out why my PopupWindow refuses to show up. 我一直在努力弄清为什么我的PopupWindow拒绝显示。 I have inflated its view and made sure that it is appearing in the correct view container. 我扩大了它的视图,并确保它出现在正确的视图容器中。

Any ideas on why the window doesn't show up? 关于为什么窗口不显示的任何想法?

Here is the code that I call in the activity where I want the popup: 这是我在想要弹出窗口的活动中调用的代码:

    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);

The XML file for my activity layout: 我的活动布局的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>

And here is my popup layout: 这是我的弹出式布局:

    <?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>

try this: 尝试这个:

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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM