本文介绍了如何在对话框中居中显示标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用中实现了该对话框。但是默认情况下对话框的标题在左侧。如何使对话框标题居中?

I have implemented the dialog in my app. But the title in the dialog by default in the left side. How can I make the dialog title in the center?

这是我的代码

final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.contact_query);
dialog.setTitle("Query Form");


推荐答案

您可以尝试以下方法:

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

这篇关于如何在对话框中居中显示标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 11:52