如何在对话框中居中标题?

时间:2016-09-03 07:17:11

标签: android dialog

我在我的应用中实现了对话框。但默认情况下左侧的对话框中的标题。如何在中心设置对话框标题?

这是我的代码

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-NO integer not null, ADDRESS varchar(60), AGE varchar(255), DATE_OF_BIRTH datet' at line 1   */    

3 个答案:

答案 0 :(得分:6)

你可以试试这个:

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

答案 1 :(得分:1)

< p>< strong> *试试这个*< / strong>< / p> <预><代码> Dialog dialog = new Dialog(this);     dialog.setContentView(R.layout.yourlayout);     TextView titleView =(TextView)dialog.findViewById(android.R.id.title);     titleView.setGravity(Gravity.CENTER); < /代码>< /预> < p>了解更多信息 < a href =“http://kodecenter.com/article?id=2390ec63-63d7-4534-a76f-cc3b10497a2c”rel =“nofollow noreferrer”> http://kodecenter.com/article?id = 2390ec63- 63d7-4534-a76f-cc3b10497a2c< / A>< / p为H.

答案 2 :(得分:0)

如果您使用AlertDialog Builder,请为对话框标题创建一个布局(例如,您可以在其中居中文本),对其进行充气并将其提供给构建器。像这样:

res / layout / dialog_title.xml:

final ResponseInfo info = responseEnvelope.body.requestData.info;
System.out.println(info.accessToken);
System.out.println(info.credentialId);

爪哇:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/colorPrimaryDark_rc"
  android:textSize="22sp"
  android:gravity="center"
  android:textColor="@color/textColorPrimaryDark"
  android:padding="10dp"/>