如何从 DialogFragment 中删除标题?

Posted

技术标签:

【中文标题】如何从 DialogFragment 中删除标题?【英文标题】:How can I Remove title from DialogFragment? 【发布时间】:2021-04-20 04:23:55 【问题描述】:

我已经尝试了一段时间来删除 DialogFragment 的标题,但几次尝试都失败了。我制作了一个 DialogFragment,它显示了用户的个人资料,但它有一个顶部标题显示。当我使用getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 时,我在对话框CardView 上显示的卡片会消失并显示简单的细节。

当我不使用 getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); DialogFragment 显示是这样的

当使用 getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); DialogFragment 显示是这样的

我希望删除标题并保留背景。

这是我的代码

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
    if (_main == null) _main = (MainActivity) getActivity();
    context = _main;

    View v = inflater.inflate(R.layout.fragprofile, container, false);

    layProfile = (ConstraintLayout) v.findViewById(R.id.layProfile);
    txtID = v.findViewById(R.id.txtID);
    txtUserID = v.findViewById(R.id.txtBenutzername);
    txtName = v.findViewById(R.id.txtName);
    txtAdresse = v.findViewById(R.id.txtAdresse);
    lineAdresse = v.findViewById(R.id.lineAdresse);
    txtFocusOfWork = v.findViewById(R.id.txtFocusOfWork);
    lineFocusOfWork = v.findViewById(R.id.lineFocusOfWork);
    txtOnlineState = v.findViewById(R.id.txtOnlineState);
    imgProfileBlurred = v.findViewById(R.id.imgProfileBlurred);
    imgProfileCircle = v.findViewById(R.id.imgProfileCircle);
    btnPhone = (TextView) v.findViewById(R.id.btnPhone);
    linePhone = v.findViewById(R.id.linePhone);
    btnEMail = (TextView) v.findViewById(R.id.btnEMail);
    lineEMail = v.findViewById(R.id.lineEMail);
    btnWhatsApp = (ImageView) v.findViewById(R.id.btnWhatsApp);

    if (this.BenutzerName != null) 
        try 
            setValuesTextAndImage(this.BenutzerID, this.BenutzerName);
         catch (Throwable throwable) 
            throwable.printStackTrace();
            lib.ShowException(TAG, context, throwable, false);
        
    

   Objects.requireNonNull(getDialog()).getWindow().setBackgroundDrawableResource(R.drawable.bg_round_corner);
    return v;

XML

<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/layProfile"
android:layout_
android:layout_
android:background="@android:color/white"
tools:context="de.com.limto.limto1.dlgProfile">

<ImageView
    android:id="@+id/imgProfileBlurred"
    android:layout_
    android:layout_
    android:scaleType="centerCrop"
    android:src="@drawable/vn_logo_c02"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/txtID"
    android:layout_
    android:layout_
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:hint="@string/ID"
    android:shadowColor="#000000"
    android:shadowDx="1.5"
    android:shadowDy="1.3"
    android:shadowRadius="1.6"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.imageview.ShapeableImageView
    android:id="@+id/imgProfileCircle"
    android:layout_
    android:layout_
    android:layout_marginTop="4dp"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    app:layout_constraintEnd_toStartOf="@id/txtOnlineState"
    app:layout_constraintHorizontal_bias="0.179"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/txtID"
    app:shapeAppearanceOverlay="@style/roundedImageViewRounded"
    app:srcCompat="@drawable/ic_launcher_background" />

<TextView
    android:id="@+id/txtOnlineState"
    android:layout_
    android:layout_
    android:gravity="center"
    android:hint="@string/onlinestate"
    android:shadowColor="#000000"
    android:shadowDx="1.5"
    android:shadowDy="1.3"
    android:shadowRadius="1.6"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="@id/imgProfileBlurred"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/txtName"
    app:layout_constraintVertical_bias="0.873" />

<TextView
    android:id="@+id/txtBenutzername"
    style="@style/TextAppearance.AppCompat.Title"
    android:layout_
    android:layout_
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:hint="@string/Benutzername"
    android:shadowColor="#000000"
    android:shadowDx="1.5"
    android:shadowDy="1.3"
    android:shadowRadius="1.6"
    android:textColor="@android:color/white"
    app:layout_constraintBottom_toTopOf="@+id/txtName"
    app:layout_constraintStart_toEndOf="@+id/imgProfileCircle"
    app:layout_constraintTop_toTopOf="@+id/imgProfileCircle" />

<TextView
    android:id="@+id/txtName"
    style="@style/TextAppearance.AppCompat.Body1"
    android:layout_
    android:layout_
    android:hint="@string/name2"
    android:shadowColor="#000000"
    android:shadowDx="1.5"
    android:shadowDy="1.3"
    android:shadowRadius="1.6"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="@+id/imgProfileCircle"
    app:layout_constraintStart_toStartOf="@id/txtBenutzername"
    app:layout_constraintTop_toBottomOf="@id/txtBenutzername" />

<TextView
    android:id="@+id/txtAdresse"
    android:layout_
    android:layout_
    android:layout_marginTop="8dp"
    android:drawablePadding="32dp"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:text="@string/Adresse"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    app:drawableStartCompat="@drawable/ic_adress"
    app:layout_constraintTop_toBottomOf="@id/imgProfileBlurred" />

<View
    android:id="@+id/lineAdresse"
    android:layout_
    android:layout_
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:background="#c0c0c0"
    app:layout_constraintTop_toBottomOf="@id/txtAdresse" />


<TextView
    android:id="@+id/txtFocusOfWork"
    android:layout_
    android:layout_
    android:layout_marginTop="16dp"
    android:drawablePadding="32dp"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:text="@string/Taetigkeitsschwerpunkte"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    app:drawableStartCompat="@drawable/ic_work"
    app:layout_constraintTop_toBottomOf="@id/lineAdresse" />

<View
    android:id="@+id/lineFocusOfWork"
    android:layout_
    android:layout_
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:background="#c0c0c0"
    app:layout_constraintTop_toBottomOf="@id/txtFocusOfWork" />

<TextView
    android:id="@+id/btnPhone"
    android:layout_
    android:layout_
    android:layout_marginTop="16dp"
    android:drawablePadding="32dp"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:text="@string/phone"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    app:drawableStartCompat="@drawable/ic_phone"
    app:layout_constraintStart_toStartOf="@id/txtFocusOfWork"
    app:layout_constraintTop_toBottomOf="@id/lineFocusOfWork" />

<ImageView
    android:id="@+id/btnWhatsApp"
    android:layout_
    android:layout_
    android:layout_marginStart="@dimen/dialog_vertical_margin"
    android:layout_marginLeft="@dimen/dialog_vertical_margin"
    android:layout_marginTop="@dimen/dialog_horizontal_margin"
    android:contentDescription="@string/whatsapp"
    android:src="@drawable/cc"
    app:layout_constraintBottom_toTopOf="@id/linePhone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintStart_toEndOf="@id/btnPhone"
    app:layout_constraintTop_toBottomOf="@id/lineFocusOfWork" />


<View
    android:id="@+id/linePhone"
    android:layout_
    android:layout_
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:background="#c0c0c0"
    app:layout_constraintTop_toBottomOf="@id/btnPhone" />

<TextView
    android:id="@+id/btnEMail"
    android:layout_
    android:layout_
    android:layout_marginTop="16dp"
    android:drawablePadding="32dp"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:text="@string/prompt_email"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    app:drawableStartCompat="@drawable/ic_email"
    app:layout_constraintTop_toBottomOf="@id/linePhone" />

<View
    android:id="@+id/lineEMail"
    android:layout_
    android:layout_
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:background="#c0c0c0"
    app:layout_constraintTop_toBottomOf="@id/btnEMail" />

</androidx.constraintlayout.widget.ConstraintLayout>

R.drawable.bg_round_corner

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <corners android:radius="20dp" />
</shape>

【问题讨论】:

在你的第一个屏幕截图中,标题不是被删除了,背景还在吗? 标题也应该被删除,圆角背景应该保留。 删除此标题后,个人资料会上升。 @Umair U 没有回复。 对不起,我忘了:)。您是否尝试过使用父布局,然后将圆形图标提供给内部布局。或者尝试为您的布局留出余量并检查它是否有效。 【参考方案1】:

正常创建你的对话框,在显示之前添加这样一行:

myDialog.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

【讨论】:

我在使用getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); DialogFragment show but without background的问题中已经提到过。【参考方案2】:

只需在约束布局中从顶部留出一点边距,您就会达到预期的效果。

【讨论】:

以上是关于如何从 DialogFragment 中删除标题?的主要内容,如果未能解决你的问题,请参考以下文章

DialogFragment 泄漏内存

侦听DialogFragment从ViewPager Fragment中删除事件

如何从Android中没有活动和片段的函数调用DialogFragment?

从 DialogFragment 回调片段

使用 DialogFragment 从布局 xml 调用方法。它是如何工作的?

从 DialogFragment 获取价值 [重复]