Добавление кнопки закрытия в DialogFragment - PullRequest
0 голосов
/ 27 августа 2018

Я пытался заставить DialogFragment иметь кнопку закрытия в левом верхнем углу, как видно на фотографии. Как должен выглядеть конечный результат (извините за текст, он на румынском языке) Может кто-нибудь сказать мне, как я могу это сделать?

Вот макет для фрагмента:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:background="#FFFFFF"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="-50dp" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sample Text"/>
    <!---add your views here-->
</LinearLayout>

<ImageView
    android:id="@+id/imageView_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    android:clickable="true"
    android:src="@drawable/ic_close_dialog" />

То, как фрагмент выглядит сейчас: Небольшое поле, в котором изображение и текст наложены

Ответы [ 4 ]

0 голосов
/ 21 июня 2019

dialog_xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@color/transparent">

    <FrameLayout
        android:id="@+id/contentView"
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <!-- you will add content here -->
    </FrameLayout>

    <ImageView
        android:id="@+id/ivCloseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:contentDescription="@string/content_description"
        app:layout_constraintBottom_toTopOf="@+id/contentView"
        app:layout_constraintEnd_toEndOf="@+id/contentView"
        app:layout_constraintStart_toEndOf="@+id/contentView"
        app:layout_constraintTop_toTopOf="@+id/contentView"
        app:srcCompat="@android:drawable/ic_menu_close_clear_cancel" />

</android.support.constraint.ConstraintLayout>

стиль:

    <style name="TransparentDialog" parent="@style/Base.Theme.AppCompat.Light.Dialog">
        <item name="colorPrimaryDark">@android:color/black</item> <!-- you can change here --> 
        <item name="android:windowBackground">@android:color/transparent</item> <!-- this is important -->
        <item name="android:windowIsFloating">false</item> <!-- this is important -->
        <item name="android:windowNoTitle">true</item> <!-- this is important -->
    </style>
0 голосов
/ 27 августа 2018

Создайте пользовательский класс диалога, который расширяет диалоговое окно, а затем используйте XML-файл, чтобы определить свой макет, как если бы вы выполняли какое-либо действие.

public class CustomDialogClass extends Dialog {

  public CustomDialogClass(Activity a) {
    super(a);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.custom_dialog);
  }
}

И ваш файл макета custom_dialog.xml определяет макет

0 голосов
/ 27 августа 2018

при нажатии кнопки используйте:

if (mProgressDialog != null) {
            if (mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
        }

ProgressDialog mProgressDialog;

0 голосов
/ 27 августа 2018

Вы можете создать собственный макет и использовать

dialog.setContentView(R.layout.custom_view);

См. пользовательский диалог с кнопкой закрытия

Если он все еще выглядит так, как на рисунке, возможно, контейнерное представление для этого макета ограничения имеет ширину и высоту wrap_content.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...