如何在Android上设置自定义对话框标题

如何在Android上设置自定义对话框标题,android,android-dialog,Android,Android Dialog,我正在android上编写一个自定义对话框。 我使用onCreateView方法实现了这一点 public class CheckpointLongPressDialog extends DialogFragment { public void CheckpointLongPressDialog() {} @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bun

我正在android上编写一个自定义对话框。 我使用onCreateView方法实现了这一点

public class CheckpointLongPressDialog extends DialogFragment {

public void CheckpointLongPressDialog() {}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_checkpoint_long_press_dialog, container);
    getDialog().setTitle("TITLE");
    return view;
}    

如何以编程方式居中标题?

也许这不是最好的方法,我使用自定义标题文本视图

TextView title = new TextView(mainActivity);
title.setText(alertTitle);
title.setBackgroundResource(R.drawable.gradient);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER); // this is required to bring it to center.
title.setTextSize(22);
getDialog().setCustomTitle(title);

也许这不是最好的方式,我使用自定义标题文本视图

TextView title = new TextView(mainActivity);
title.setText(alertTitle);
title.setBackgroundResource(R.drawable.gradient);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER); // this is required to bring it to center.
title.setTextSize(22);
getDialog().setCustomTitle(title);
试试这个

final int titleId = getActivity().getResources().getIdentifier("alertTitle", "id", "android");
TextView title = (TextView) getDialog().findViewById(titleId);
if (title != null) {
    title.setGravity(Gravity.CENTER);
}
试试这个

final int titleId = getActivity().getResources().getIdentifier("alertTitle", "id", "android");
TextView title = (TextView) getDialog().findViewById(titleId);
if (title != null) {
    title.setGravity(Gravity.CENTER);
}

如果您使用整个版面来扩大您的自定义标题会怎么样?。而不是
getDialog().setTitle(“TITLE”)您还可以在标题的自定义版面中包含文本视图。

如果您使用整个版面来扩大自定义标题,会怎么样?。而不是
getDialog().setTitle(“TITLE”)您还可以在标题的自定义布局中包含文本视图。

我使用生成器和扩展xml布局来解决此问题

private AlertDialog.Builder builder;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.fragment_checkpoint_long_press_dialog, null));


    // Create the AlertDialog object and return it
    return builder.create();
}

我使用构建器和膨胀xml布局来解决这个问题

private AlertDialog.Builder builder;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.fragment_checkpoint_long_press_dialog, null));


    // Create the AlertDialog object and return it
    return builder.create();
}

标题视图使用默认主题。您有两种方法可以实现自己的目标,第一种方法更适合于定制体验:

  • 使用此选项可以创建一个没有标题的对话框,然后在此片段的布局中创建自定义标题栏

    dialog.getWindow().requestFeature(Window.FEATURE\u NO\u TITLE)

  • 扩展并更新对话框的默认主题,然后在此对话框中进行设置


  • 标题视图使用默认主题。您有两种方法可以实现自己的目标,第一种方法更适合于定制体验:

  • 使用此选项可以创建一个没有标题的对话框,然后在此片段的布局中创建自定义标题栏

    dialog.getWindow().requestFeature(Window.FEATURE\u NO\u TITLE)

  • 扩展并更新对话框的默认主题,然后在此对话框中进行设置


  • 当您可以简单地更改
    片段检查点长按按钮对话框
    xml文件时,为什么要通过编程方式来强调自己呢?可能是@DerGolem的重复您是对的,但我认为有一种简单的编程方式可以做到这一点,在xml中这样做可能更简单(1行代码)。当您可以简单地更改
    片段检查点长按按钮\u对话框
    xml文件时,为什么要通过编程来强调自己呢?可能重复@DerGolem您是对的,但我认为有一种简单的方法可以通过编程实现这一点,在xml中实现这一点可能更简单(1行代码)。我用这种方法尝试,但找不到函数
    setCustomTitle(title)
    我用这种方法尝试,但找不到函数
    setCustomTitle(title)