Android popupWindow不';你不来吗?

Android popupWindow不';你不来吗?,android,popupwindow,Android,Popupwindow,我试图创建一个弹出窗口,将显示在按下一个按钮。 我找到了很多教程并搜索了StackOverflow,但没有一个解决方案对我有帮助,我也不知道为什么 这是我的密码: LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); View newValueView = inflater.inflate(R.layout.change_valu

我试图创建一个弹出窗口,将显示在按下一个按钮。 我找到了很多教程并搜索了StackOverflow,但没有一个解决方案对我有帮助,我也不知道为什么

这是我的密码:

    LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View newValueView = inflater.inflate(R.layout.change_value_popup,null);
    newValuePopupWindow = new PopupWindow(newValueView, RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    newValuePopupWindow.showAtLocation(findViewById(R.id.main),Gravity.CENTER,0,0);
这似乎不起作用,我试图补充:

newValuePopupWindow.setFocusable(true);
但这也没用

以下是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:background="#CCE5FF"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/enter_value_title_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="Enter new value"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/new_value_editText"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<EditText
    android:id="@+id/new_value_editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="500dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="OK"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</RelativeLayout>

我试过用RelativeLayout和ConstraintLayout


是否有可能在主布局后面出现弹出窗口?

我将为您编写一个简单的弹出窗口,希望它能帮助您

您必须在AndroidManifest.xml文件中添加以下行:

<activity
        android:name=".Pop"
        android:theme="@style/AppTheme.CustomTheme" />
最后,我在单击按钮时打开弹出窗口,下面是代码(加载到MainActivity.java文件):


我会为你写一个简单的弹出窗口,我希望它会帮助你

您必须在AndroidManifest.xml文件中添加以下行:

<activity
        android:name=".Pop"
        android:theme="@style/AppTheme.CustomTheme" />
最后,我在单击按钮时打开弹出窗口,下面是代码(加载到MainActivity.java文件):

做上面的更改并尝试


进行上述更改并重试。

出现问题的原因是重复的布局(用于横向)。 出于某种原因,在其中一个布局中删除了指向onClick方法的链接(我总是选中主布局)。我犯了这么愚蠢的错误:/
感谢您的帮助:)

出现问题的原因是重复的布局(用于横向)。 出于某种原因,在其中一个布局中删除了指向onClick方法的链接(我总是选中主布局)。我犯了这么愚蠢的错误:/
谢谢您的帮助:)

试试这个例子:-这是一个不同的问题,但谢谢您的帮助!试试这个例子:-这是一个不同的问题,但谢谢你的帮助!我真的很感谢你的帮助,但我已经设法用一种更简单的方式解决了这个问题。谢谢!:)我真的很感谢你的帮助,但我已经设法用一种更简单的方式解决了这个问题。谢谢!:)这是一个不同的问题,但谢谢你的帮助!这是一个不同的问题,但谢谢你的帮助!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Pop">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="217dp"
    android:text="@string/asdfdasdasdasdas" />
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;

public class Pop extends Activity{

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popwindow);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int)(width*.7),(int) (height*.7));
}
}
 Button button = findViewById(R.id.info);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, com.example.user_pc.zavrsnitri.Pop.class);
            startActivity(intent);
        }
    });
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View newValueView = inflater.inflate(R.layout.change_value_popup,null);
newValuePopupWindow = new PopupWindow(newValueView, RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
newValuePopupWindow.showAtLocation(newValueView,Gravity.CENTER,0,0);