带有自定义标题的DialogFragment

daz*_*ito 2 android android-layout android-fragments android-dialogfragment

我需要为我的自定义标题DialogFragment.

getDialog().setTitle("My Title");
Run Code Online (Sandbox Code Playgroud)

对我来说还不够,因为我想改变标题文字和背景颜色.

正如您从下图所示,getDialog().setTitle("My Title");我没有自定义(据我所知),即文本颜色为黑色,背景为白色.

我的目标是My Title定制如下所示Custom Title.我一直在寻找API,我找不到任何方法将视图加载到标题部分.

任何人都知道如何自定义或设置/注入视图FragmentDialog

在此输入图像描述

Joe*_*her 8

您必须通过覆盖onCreateDialog来创建自定义对话框,如下所示:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    dialogView = inflater.inflate(R.layout.dialog_change_password, null);

    /* Anything that needs to be done to the view, ie. 
    on click listeners, postivie/negative buttons,
    values being changed etc */

    builder.setView(dialogView);
    return builder.create();
}
Run Code Online (Sandbox Code Playgroud)

和dialog_change_password.xml:

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/white_grey">

    <TextView
        android:text="@string/change_password"
        android:id="@+id/title"
        style="@style/dialogHeading" />


    <LinearLayout
        android:layout_marginTop="10dp"
        style="@style/dialogSection">

        <LinearLayout
            style="@style/PasswordChangeRow">

            <TextView
                style="@style/PasswordChangeLabel"
                android:text="@string/new_password"/>

            <EditText
                style="@style/PasswordChangeField"
                android:id="@+id/password"
                android:inputType="textPassword"/>

        </LinearLayout>

        <LinearLayout
            style="@style/PasswordChangeRow">

            <TextView
                style="@style/PasswordChangeLabel"
                android:text="@string/confirm_password"/>

            <EditText
                style="@style/PasswordChangeField"
                android:id="@+id/confirmPassword"
                android:inputType="textPassword"/>

        </LinearLayout>
    </LinearLayout>

    <TextView
        android:text="@string/password_conditions_message"
        style="@style/dialogMessage" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

显然xml有点复杂,需要什么,但只是可以做的一个例子,它看起来像这样

在此输入图像描述