Android 向DialogFragment添加关闭按钮

Android 向DialogFragment添加关闭按钮,android,android-fragments,Android,Android Fragments,我一直在尝试让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_par

我一直在尝试让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" />

您可以创建自定义布局并使用

dialog.setContentView(R.layout.custom_view);
提及


如果它仍然与图片中的一样,则可能是此约束布局的容器视图将wrap_内容作为其宽度和高度。

创建一个扩展对话框的自定义对话框类,然后使用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
layout文件在按钮上定义布局

,单击使用:

if (mProgressDialog != null) {
            if (mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
        }
进程对话框

对话框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>

@android:彩色/黑色
@android:彩色/透明
假的
真的

我之前在看那篇文章,试图找到经过验证的答案。不幸的是,无论我做了什么,按钮都不会移动到顶部。(完整)布局已发布请发布完整的XML文件。似乎丢失了一些内容在预览中似乎很好,图像与左上方对齐在预览中对您合适吗?如果我需要的是DialogFragment,ProgressDialog在这种情况下有何帮助?