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

时间:2015-02-11 09:58:19

标签: android android-dialog

我在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;
}    

如何以编程方式居中?

5 个答案:

答案 0 :(得分:3)

也许它不是最好的方式,我使用自定义标题TextView。

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

答案 1 :(得分:1)

我使用构建器解决了这个问题并对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();
}

答案 2 :(得分:0)

试试这个..

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

答案 3 :(得分:0)

如果您使用整个布局来充气自定义标题怎么办?您可以在自定义布局中为标题添加TextView,而不是getDialog().setTitle("TITLE");

答案 4 :(得分:0)

标题视图使用默认主题。你有2种方法可以做你想做的事情,第一种方法更适合提供更加个性化的体验:

  1. 使用此对话框没有标题,然后在此片段的布局中制作自定义标题栏。

    dialog.getWindow()requestFeature(Window.FEATURE_NO_TITLE);

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

相关问题