android - 向 DialogFragment 添加关闭按钮

标签 android android-fragments

我一直在尝试使 DialogFragment 在左上角有一个关闭按钮,如图所示。 What the final result should look like (Sorry about the text, it's in romanian)谁能告诉我该怎么做?

这是 fragment 的布局:

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

fragment 现在的样子:A small box where the image and text are overlayed

最佳答案

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

dialog.setContentView(R.layout.custom_view);

引用custom dialog with close button

如果仍然像图片中那样,则可能是此约束布局的容器 View 将wrap_content作为其宽度和高度。

关于android - 向 DialogFragment 添加关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52041014/

相关文章:

Android - 选项卡式 fragment

android - 错误 : unknown type name 'class' NDK CDT JNI

java - 下载目录下写问题

android - 带有 ViewPager 的 TabLayout 不能在 fragment Android 中工作

java - setPreferencesFromResource 与 addPreferencesFromResource

android - 使用 Android 将特定于供应商的信息插入并嗅探到信标帧中

java - Android 中的 Activity 发生变化

android - 如何从首选项屏幕打开 android 文本到语音设置?

android - 使用ViewPager时如何处理savedInstanceState?

java - 如何将 'creating of a new instance of a fragment' 保存在变量中?