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
在我要设置样式的对话框的onCreateD