DialogFragment with a custom title

百般思念 提交于 2019-12-22 12:15:13

问题


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.

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 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.

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


回答1:


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

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>

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




回答2:


You can use setCustomTitle method, where you can inflate your own fully customized layout

Example

import android.support.v4.app.DialogFragment;

public class PromoDetailDialogFragment extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View titleView = getActivity().getLayoutInflater().inflate(R.layout.my_title, null);

        TextView tv = (TextView) titleView.findViewById(R.id.my_title_textView);
        tv.setText("title");

        AlertDialog dialog = new AlertDialog.Builder(getContext())
                .setCustomTitle(titleView)
                .setMessage("message")
                .setCancelable(true)
                .create();

        return dialog;
    }
}

This way is cleaner and much more powerful



来源:https://stackoverflow.com/questions/28977576/dialogfragment-with-a-custom-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!