无法解析方法 showAtLocation

Method showAtLocation cannot be resolved

我正在尝试创建一个来自屏幕右下角按钮的弹出菜单。问题是菜单需要显示在按钮上方。我把 Android. How to show popup window directly above button. However, the showAtLocation method does not resolve. Another issue that I believe is related to this is that the activity crashes ever since I added my onMenuItemClick() switch statements. Any help would be greatly appreciated :) and more code can be found at my Github

中的一些代码拼凑在一起

--编辑---

原来 showAtLocation 只适用于弹出菜单 windows,但是我使用的是弹出菜单。所以问题就变成了,如何在按钮上方而不是下方显示弹出窗口?

WorkoutsCreater.java:

package com.example.workoutapp;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class WorkoutsCreater extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_workouts_creater);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Workout Creater");                                      //change text at top of screen accordingly
        Button btn=findViewById(R.id.BtnNew);
        btn.setOnClickListener(new View.OnClickListener(){
           @Override
           public void onClick(View v){
               PopupMenu popup = new PopupMenu(WorkoutsCreater.this, v);                                    //display menu when button clicked
               popup.setOnMenuItemClickListener(WorkoutsCreater.this);
               popup.inflate(R.menu.workout_new_popup_menu);
               popup.showAtLocation(v, Gravity.TOP, 0, (int) v.getY());               //show popup above button
           }
        });
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
        switch (item.getItemId()) {                                                                                             //testing which button is pressed in menu
            case R.id.RepBased:
                System.out.println("Rep Based");
                return true;
            case R.id.RunBased:
                System.out.println("Run Based");
                return true;
            case R.id.TimeBased:
                System.out.println("Time Based");
                return true;
            default:
                return false;
        }
    }
}

workout_new_popup_menu.xml:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/RepBased"
        android:icon="@drawable/ic_workouts"
        android:title="Rep Based" />
    <item android:id="@+id/RunBased"
        android:icon="@drawable/ic_run_black"
        android:title="Run Based" />
    <item android:id="@+id/TimeBased"
        android:icon="@drawable/ic_time_based"
        android:title="Time Based" />
</menu>

activity_workouts_creater.xml:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WorkoutsCreater">


    <TextView
        android:id="@+id/NameWorkoutTextView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:gravity="center"
        android:text="Name of Workout:"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textSize="15dp"
        app:layout_constraintBottom_toBottomOf="@+id/NameWorkoutInput"
        app:layout_constraintEnd_toStartOf="@+id/NameWorkoutInput"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/NameWorkoutInput" />

    <EditText

        android:id="@+id/NameWorkoutInput"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="40dp"
        android:layout_marginRight="40dp"
        android:inputType="text"
        android:maxLength="20"
        android:textSize="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/NameWorkoutTextView"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/BtnNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_add_white" />



</androidx.constraintlayout.widget.ConstraintLayout>

showAtLocation()PopupWindow 方法,而不是 PopupMenu 方法。 您复制的代码也在 PopupWindow 上使用它。

您可以阅读更多关于 PopupWindow here and about the specific method here

v.setOnTouchListener(popup.getDragToOpenListener()); 修复了弹出问题和崩溃是由于使用错误的按钮类型引起的。原来你不能使用带有弹出菜单的浮动操作按钮