popupWindow не показывает? - PullRequest
0 голосов
/ 06 мая 2018

Я пытаюсь создать всплывающее окно, которое будет отображаться при нажатии кнопки. Я нашел много учебников и искал 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.

Есть ли вероятность, что всплывающее окно появляется за основным макетом?

Ответы [ 3 ]

0 голосов
/ 06 мая 2018
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);

внесите изменения и попробуйте.

0 голосов
/ 06 мая 2018

Проблема возникла из-за дублированного макета (для ландшафта).По какой-то причине ссылка на метод onClick удалена в одном из макетов (я всегда проверял основной).Такая глупая ошибка со мной: / Спасибо за помощников:)

0 голосов
/ 06 мая 2018

Я напишу для вас одно простое всплывающее окно, надеюсь, оно вам поможет.

Вы должны добавить следующие строки в свой файл AndroidManifest.xml:

<activity
        android:name=".Pop"
        android:theme="@style/AppTheme.CustomTheme" />

Здесь я делаю простую тему для всплывающего окна, мы можем изменить это ... мы определяем это в файле styles.xml:

<style name="AppTheme.CustomTheme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
</style>

Вы также сделали один макет для всплывающего окна, вот код для макета:

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

Также вы должны сделать файл Java, вот код для этого:

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

В конце я открываю всплывающее окно при нажатии кнопки, вот код (добавить в файл MainActivity.java):

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