如何在DialogFragment中自定义标题

时间:2016-07-14 18:46:39

标签: java android android-fragments

我想在我的DialogFragment中自定义我的标题,我知道如何将标题设置为我的DialogFragment,但我想要的不仅仅是标题,我需要一个自定义标题(颜色,fontSize ...),所以我定义了TextView并对其进行了自定义

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.height_fragment,container,false);

    TextView title =  new TextView(getContext());
    title.setText("Your Height Information");
    title.setGravity(Gravity.CENTER);
    title.setTextSize(30);
    title.setBackgroundColor(Color.GRAY);
    title.setTextColor(Color.WHITE);

    getDialog().setCustomTitle(title); //Error Can not resolve method setCustomTitle(android.widget.TextView)

    return view;
}

但此行存在错误

getDialog().setCustomTitle(title);

getDialog()setCustomTitle(标题);

我在发布此问题之前搜索了很多但我找不到如何自定义DialogFragment的标题。 感谢您花时间帮助我

4 个答案:

答案 0 :(得分:4)

“getDialog”没有方法“setCustomTitle”。此方法只能用于AlertDialog.Builder。试试这样:

getDialog().setTitle("Your Height Information");
TextView textView=(TextView) getDialog().findViewById(android.R.id.title);
textView.setTextSize(30);

答案 1 :(得分:0)

我永远无法轻松自定义DialogFragment的标题。因此,我使用删除标题并添加TextView到Dialog布局(并像标题一样使用它)。这样,您就可以轻松自定义。

对话代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Remove TITLE
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);


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

    // Do your stuff

    return dialogView;
}

height_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="56dp"

        <!-- THIS VIEW IS YOUR TITLE... CUSTOMIZE LYKE YOU WANT -->
        android:textColor="@color/white"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_gravity="top"

        android:text="@string/TITLE"
        android:gravity="center_vertical"
        android:paddingLeft="16dp" />

    <!-- Real Content goes below the title -->
</FrameLayout>

答案 2 :(得分:0)

您需要将xml连接到您的班级。 findViewById告诉您的班级TextView使用哪个android:id。请注意它必须与xml中的public class DialogFragmentTest extends DialogFragment { public final static String TITLE = "title"; public final static String MESSAGE = "message"; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Get the layout inflater LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.dialog, null); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder.setView(view); Bundle args = getArguments(); String title = args.getString(TITLE, "default title"); String msg = args.getString(MESSAGE, "default message"); TextView tvTitle = (TextView)view.findViewById(R.id.dialog_title); tvTitle.setText(title); TextView tvMessage = (TextView)view.findViewById(R.id.dialog_message); tvMessage.setText(msg); Button btnDone = (Button)view.findViewById(R.id.dialog_button); btnDone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); return builder.create(); } } 匹配:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="300dp"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="title"/>
    <TextView
        android:id="@+id/dialog_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dialog_title"
        android:layout_marginTop="10dp"
        android:text="message"/>
    <Button
        android:id="@+id/dialog_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/dialog_message"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:text="done"/>


</RelativeLayout>

dialog.xml:

DialogFragmentTest bd = new DialogFragmentTest();
Bundle b = new Bundle();
b.putString(DialogFragmentTest.TITLE, "my title");
b.putString(DialogFragmentTest.MESSAGE, "my message");
bd.setArguments(b);
bd.show(getFragmentManager(), "my title");

然后通过以下方式从您的活动中调用:

#problem-panels {
  margin-bottom:0;
}

答案 3 :(得分:0)

请使用基于DialogFragment的自定义对话框:

public class MyCustomDialog extends DialogFragment{
    private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.custom_dialog, null, false);
        TextView tvTitle = (TextView) getDialog().findViewById(android.R.id.title);
        tvTitle.setTextSize(25);
        tvTitle.setTextColor(Color.GREEN);

        return dialogView;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(this.context, R.style.CustomDialog);
        dialog.setTitle("custom dialog title");
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return dialog;
    }

    public static MyCustomDialog newInstance() {

        MyCustomDialog myCustomDialog = new MyCustomDialog();
        return myCustomDialog;
    }
}