弹出菜单未在锚点视图android上设置

时间:2020-04-23 06:49:55

标签: java android view center popupwindow

我想在要放大弹出式布局的视图上显示一个弹出菜单。我正在为弹出功能使用弹出类。目前,我无法在视图上看到弹出窗口。

我检查了堆栈溢出的许多答案,并尝试应用对我仍然无效的答案。

这是我的弹出课程:

    public class PopUpClass {

        //PopupWindow display method

        //Create a View object yourself through inflater
        LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popup_menu_layout, null);


        //Specify the length and width through constants
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;

        //Make Inactive Items Outside Of PopupWindow
        boolean focusable = true;

        PopupWindow popupWindow = new PopupWindow(popupView,
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);



/*       popupWindow.showAtLocation(view,Gravity.RIGHT,
                view.getWidth() - (view.getWidth()*50)/100,
                view.getHeight() - (view.getHeight()*10)/100);

        popupWindow.showAsDropDown(view, 0,
                -view.getHeight()-popupView.getHeight());*/

        //Create a window with our parameters
  /*      final PopupWindow popupWindow = new PopupWindow(popupView, width,
                height, focusable);*/



     //   Set the location of the window on the screen
/*    popupWindow.showAtLocation(view,
            view.getWidth() - popupWindow.getWidth(),
               view.getWidth(), view.getHeight());*/
    }

我通过提供被单击的视图来调用适配器中的弹出窗口。

holder.imageViewShare.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {

                        PopUpClass popUpClass = new PopUpClass();
                        popUpClass.showPopupWindow(v);
            }
        });

这是我的片段布局,其中包括共享之类的视图。我想在imageViewShare上打开弹出窗口

http://schemas.android.com/apk/res/android“ xmlns:app =“ http://schemas.android.com/apk/res-auto” xmlns:tools =“ http://schemas.android.com/tools” android:layout_width =“ match_parent” android:layout_height =“ match_parent” 工具:上下文=“ .QuotesFragment” android:background =“ @ drawable / bubbles_theme”>

        <RelativeLayout
            android:layout_width = "match_parent"
            android:layout_height = "match_parent"
            app:layout_constraintBottom_toBottomOf = "parent"
            app:layout_constraintEnd_toEndOf = "parent"
            app:layout_constraintStart_toStartOf = "parent"
            app:layout_constraintTop_toTopOf = "parent">

            <TextView
                android:id = "@+id/textView_quotes"
                android:layout_width = "250dp"
                android:layout_height = "300dp"
                android:layout_centerInParent = "true"
                android:fontFamily = "@font/open_sans_semibold_italic"
                android:gravity = "start|center_vertical"
                android:text = "Doing a same thing over and over again and expecting different results"
                android:textAlignment = "textStart"
                android:autoSizeTextType="uniform"
                android:textColor = "@android:color/white"
                android:textSize = "@dimen/text_size24" />

            <include
                android:id = "@+id/include2"
                layout = "@layout/share_save_like_layout"
                android:layout_width = "match_parent"
                android:layout_height = "60dp"
                android:layout_below = "@id/textView_quotes"
                android:layout_marginTop="@dimen/margin_20"
                android:layout_centerHorizontal = "true"
                app:layout_constraintEnd_toEndOf = "parent"
                app:layout_constraintStart_toStartOf = "parent" />

        </RelativeLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

这是share_save_like_layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_width = "wrap_content"
    android:layout_height = "60dp">

    <ImageView
        android:id = "@+id/imageView_favourite"
        android:layout_width = "30dp"
        android:layout_height = "30dp"
        android:src = "@drawable/ic_favourite"
        android:layout_marginEnd="60dp"
        android:layout_marginRight = "60dp"
        android:layout_marginLeft="60dp"
        android:layout_marginStart="60dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@id/imageView_bookmark"
       />

    <ImageView
        android:id = "@+id/imageView_bookmark"
        android:layout_width = "30dp"
        android:layout_height = "30dp"
        android:src = "@drawable/ic_bookmark"
        android:layout_marginEnd="60dp"
        android:layout_marginRight = "60dp"
        android:layout_marginLeft="60dp"
        android:layout_marginStart="60dp"
        app:layout_constraintStart_toStartOf="@id/imageView_favourite"
        app:layout_constraintEnd_toEndOf="@id/imageView_share"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <ImageView
        android:id = "@+id/imageView_share"
        android:layout_width = "30dp"
        android:layout_height = "30dp"
        android:src = "@drawable/ic_share"
        android:layout_marginEnd="60dp"
        android:layout_marginRight = "60dp"
        android:layout_marginLeft="60dp"
        android:layout_marginStart="60dp"
        app:layout_constraintStart_toStartOf="@id/imageView_bookmark"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

请对此进行指导。

这是它的外观 enter image description here

请帮助提供建议。谢谢。

0 个答案:

没有答案
相关问题