android 自定义dialog居中,Android上的自定义对话框:如何居中显示标题?

在Android开发中,自定义对话框时可能会遇到标题居中的需求。本文介绍了多种将自定义对话框标题居中的方法,包括通过setCustomTitle()设置自定义标题、在代码中设置Gravity.CENTER以及通过样式XML进行配置。这些解决方案提供了灵活的方式来实现对话框标题的居中显示。

Android上的自定义对话框:如何居中显示标题?

我正在开发一个Android应用程序。

如何将正在使用的自定义对话框的标题居中?

谢谢。

11个解决方案

100 votes

可以通过编程方式完成此操作的另一种方法是使用setCustomTitle():

// Creating the AlertDialog with a custom xml layout (you can still use the default Android version)

AlertDialog.Builder builder = new AlertDialog.Builder(this);

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = inflater.inflate(R.layout.viewname, null);

builder.setView(view);

TextView title = new TextView(this);

// You Can Customise your Title here

title.setText("Custom Centered Title");

title.setBackgroundColor(Color.DKGRAY);

title.setPadding(10, 10, 10, 10);

title.setGravity(Gravity.CENTER);

title.setTextColor(Color.WHITE);

title.setTextSize(20);

builder.setCustomTitle(title);

LandL Partners answered 2019-10-02T05:16:42Z

56 votes

只是在试图弄清楚如何做相同的事情的同时找到了这篇文章。 这是我为将来会发现此问题的其他人所做的方法。

样式xml如下:

@style/PauseDialogTitle

center_horizontal

1

true

@android:style/TextAppearance.DialogWindowTitle

在我要设置样式的对话框的onCreateDialog方法的活动中,我像这样创建对话框:

Dialog pauseDialog = new Dialog(this, R.style.PauseDialog);

pauseDialog.setTitle(R.string.pause_menu_label);

pauseDialog.setContentView(R.layout.pause_menu);

ChrisJD answered 2019-10-02T05:16:19Z

8 votes

您也可以在代码中完成。 假设您有对话框片段,然后添加以下代码行。

@Override

public void onStart()

{

super.onStart();

TextView textView = (TextView) this.getDialog().findViewById(android.R.id.title);

if(textView != null)

{

textView.setGravity(Gravity.CENTER);

}

}

Hesam answered 2019-10-02T05:17:06Z

2 votes

您可以通过编程方式进行操作,而无需自定义视图:

@Override

public void onStart()

{

super.onStart();

TextView textViewVanilla = (TextView) this.getDialog().findViewById(android.R.id.title);

if(textViewVanilla != null)

{

textViewVanilla.setGravity(Gravity.CENTER);

}

// support for appcompat v7

TextView textViewAppcompat = (TextView) this.getDialog().findViewById(android.support.v7.appcompat.R.id.alertTitle);

if(textViewAppcompat != null)

{

textViewAppcompat.setGravity(Gravity.CENTER);

}

}

感谢@hesam的想法。 有关appcompat布局,请参见Android/sdk/platforms/android-26/data/res/layout/alert_dialog_title_material.xml

soshial answered 2019-10-02T05:17:37Z

1 votes

如果您不拨打AlertDialog.Builder.setIcon()和AlertDialog.Builder.setTitle(),则您的自定义对话框将不会显示内置/默认标题视图。 在这种情况下,您可以添加自定义标题视图:

AlertDialog.Builder.setView(View view)

只要是您创建此视图,就可以实现任何类型的对齐方式。

Vit Khudenko answered 2019-10-02T05:18:10Z

1 votes

对于您的自定义DialogFragment,您可以执行以下操作:

@Override

public Dialog onCreateDialog(Bundle savedInstanceState) {

final Dialog dialog = super.onCreateDialog(savedInstanceState);

final TextView textView = (TextView) dialog.findViewById(android.R.id.title);

if(textView != null) {

textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

}

return dialog;

}

Nail Sharipov answered 2019-10-02T05:18:34Z

1 votes

TextView titleView = (TextView) dialog.findViewById(android.R.id.title);

if(titleView != null) {

titleView.setGravity(Gravity.CENTER);

}

有关更多详细信息,请参见Android Dialog和AlertDialog上的这篇KodeCenter文章。

user7859337 answered 2019-10-02T05:18:59Z

0 votes

您这里有一些修改对话框标题的入门提示:Android-在运行时更改自定义标题视图不知道它是否可以居中(尚未尝试过),但是如果它是自定义视图,我想这很有可能。

Vuk answered 2019-10-02T05:19:26Z

0 votes

这是一个令人讨厌的解决方案。...扩展AlertDialog.Builder并覆盖所有方法(例如setText,setTitle,setView等),以不设置实际对话框的文本/标题/视图,而是在对话框的视图中创建一个新视图 在那里做所有的事情。 然后,您可以随意设置所有样式。

为了澄清起见,就父类而言,已设置视图,而没有其他设置。

就您的自定义扩展类而言,一切都在该视图中完成。

Steven L answered 2019-10-02T05:20:04Z

0 votes

试试这个:

TextView titleText = (TextView) helpDialog.findViewById(R.id.alertTitle);

if(titleText != null) {

titleText.setGravity(Gravity.CENTER);

}

完整代码(使用android.support.v7.app.AlertDialog):

AlertDialog.Builder helpDialogBuilder = new AlertDialog.Builder(context)

.setTitle(/*your title*/)

.setMessage(/*your message*/)

.setNegativeButton("Cancel",

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

/*you can do something here*/

dialog.dismiss();

}

})

.setPositiveButton("OK",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

/*you can do something here*/

dialog.dismiss();

}

});

final AlertDialog helpDialog = helpDialogBuilder.create();

helpDialog.setOnShowListener(new DialogInterface.OnShowListener() {

@Override

public void onShow(DialogInterface dialog) {

TextView titleText = (TextView) helpDialog.findViewById(R.id.alertTitle);

if(titleText != null) {

titleText.setGravity(Gravity.CENTER);

}

TextView messageText = (TextView) helpDialog.findViewById(android.R.id.message);

if(messageText != null) {

messageText.setGravity(Gravity.CENTER);

}

}

});

helpDialog.show();

Vitaly Zinchenko answered 2019-10-02T05:20:30Z

0 votes

AlertDialog alertDialog = new AlertDialog.Builder(activity)

.setMessage(message)

.create();

alertDialog.setIcon(R.mipmap.ic_launcher_round);

@SuppressLint("RestrictedApi")

DialogTitle titleView=new DialogTitle(activity);

titleView.setText(title);

titleView.setPaddingRelative(32,32,32,0);

alertDialog.setCustomTitle(titleView);

Ahmad Aghazadeh answered 2019-10-02T05:20:48Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值