带有自定义标题的DialogFragment [英] DialogFragment with a custom title

查看:86
本文介绍了带有自定义标题的DialogFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为DialogFragment放置一个自定义标题.

I need to place a custom title to my DialogFragment.

getDialog().setTitle("My Title");

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

is not enough for me because I want to change the title text and background colors.

从下面的图片中可以看到,对于getDialog().setTitle("My Title");,我没有进行自定义(据我所知),即文本颜色为黑色,背景为白色.

As you can see from the image below, with getDialog().setTitle("My Title"); I've no customization (as far as I know), ie, the text color is black and the background is white.

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

My objective is to have My Title customized as seen by Custom Title. I've been looking around the API and I couldn't find any method to load a view into the title section.

任何人都知道如何自定义视图或将视图设置为FragmentDialog?

Anyone knows how can I customize or set/inject a view into the FragmentDialog?

推荐答案

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

You have to create a custom dialog by overiding onCreateDialog like so:

@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();
}

和dialog_change_password.xml:

and the 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>

很明显,xml比所需的要复杂一些,但这只是可以完成的示例,看起来像这样

Obviously that xml is a bit more complex that what is needed but is just an example of what can be done, it looks like this

这篇关于带有自定义标题的DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆