更改警示对话框的标题字体

26

我想要改变Alert对话框标题的字体

有人能告诉我怎么做吗?


http://stackoverflow.com/questions/16534888/how-can-i-create-my-own-theme-for-alertdialog/16534981#16534981 - Sam
有没有简单的方法,而不是创建自定义对话框? - Eagle_Eye
不,我想要改变标题的字体。 - Eagle_Eye
14个回答

31

我发现了一个简单的解决方案。

起初,我通过以下方式设置警报框的标题:

builder.setTitle("My Title");

所以我无法更改它的字体..

那么对我有用的是..

我创建了一个简单的TextView:

TextView tv2;

设置我想要的TextView的所有属性...

然后我替换了我的

builder.setTitle("My Title");

行与行之间的距离

builder.setCustomTitle(tv2);

现在我可以通过更改tv2的属性来更改标题颜色字体等。


根据文档...这也会更改对话框图标...因此在执行之前请考虑清楚... - superUser
2
那么,按钮怎么样?我该如何更改它们的颜色或按钮? - Mina Dahesh
@Eagle_Eye,你有没有想法在底部弹出式对话框中如何实现这个? - Raunak Pandey

17

没有一个答案提供了改变AlertDialog标题字体的方法。这是我所做的:

创建如下类:

public static class TypefaceSpan extends MetricAffectingSpan {

  private final Typeface typeface;

  public TypefaceSpan(Typeface typeface) {
    this.typeface = typeface;
  }

  @Override public void updateDrawState(TextPaint tp) {
    tp.setTypeface(typeface);
    tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
  }

  @Override public void updateMeasureState(TextPaint p) {
    p.setTypeface(typeface);
    p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
  }

}

添加以下实用方法:

/**
 * <p>Return spannable string with applied typeface in certain style</p>
 *
 * https://dev59.com/N2oy5IYBdhLWcg3wWsw_
 *
 * @param typeface
 *     The typeface to set to the {@link SpannableString}
 * @param string
 *     the string to place in the span
 * @return SpannableString that can be used in TextView.setText() method
 */
public static SpannableString typeface(Typeface typeface, CharSequence string) {
  SpannableString s = new SpannableString(string);
  s.setSpan(new TypefaceSpan(typeface), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  return s;
}

最后,在创建您的AlertDialog时设置字体:

Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/your_font.ttf");
new AlertDialog.Builder(getActivity())
    .setTitle(FontUtils.typeface(typeface, "The title"))
    /* .. */
    .create();

完美的解决方案。它运行得非常好。感谢Jared提供的代码!:) - Kinjal
这是一个完美的解决方案,Jared,但对于正/负按钮标签并不适用。你有类似的解决方案吗? - Parissa Kalaee
1
DialogFragment#onStart() 中,您可以获取对按钮的引用并设置字体。例如:((AlertDialog getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setTypeface(tf) - Jared Rummler
@ParissaKalaee 它对按钮也适用 - vishnu satheesh

9

只需使用以下代码行即可获取对话框标题的标识符:

int dialogTitle = mCtnx.getResources().getIdentifier( "alertTitle", "id", "android" );

使用android.support.v7.app.AlertDialog来实现对话框。

TextView title= (TextView)alertDialog.findViewById(R.id.alertTitle);


1
是的。只想补充一下...你可能会这样做:(TextView)findViewById(dialogTitle),因为这个alertTitle是从TextView继承而来的... - superUser
这对于获取“EmailPreference”对话框的标题非常有效。 - Eric B.
太好了!也适用于Unity Android Plugin Library的本地Java代码 - Junaid Pathan

3

请按照以下步骤操作:

Dialog dialog = new Dialog(ActivityName.this);
dialog.setContentView(R.layout.dialog_name);
dialog.setCancelable(true);
dialog.findViewById(R.id.text).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));

将您的font.ttf文件放在assets文件夹中,并像上面那样使用它。

1
我认为这是适当的方法,但我想建议一个更正: TextView title =(TextView)dialog.findViewById(android.R.id.title);title.setTypeface(Typeface.createFromAsset(getAssets(),"font.ttf")); - Pavan
@Gaurav Arora,你有没有想法如何在底部对话框中更改字体? - Raunak Pandey

2

您需要填充或自定义并创建样式,然后将其应用于AlertDialog

以下是如何填充布局并将其应用于AlertDialog:

LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);

AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Formatted");
builder.setView(view);

定义所有在指定布局中所需的格式和样式。

您可以使用充气的视图访问在布局中定义的特定文本视图,即:

LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);
TextView label=(TextView)view.findViewById(R.id.i_am_from_formatted_layout_lable);

示例布局保存为res/layout/link.xml:

在您的onCreate()或任何您想要调用AlertDialog的地方或时间。

LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.link, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Formatted");
builder.setView(view).create().show();
TextView text=(TextView) findViewById(R.id.text);

如果您从其他方法调用,请将this替换为上下文对象。


但它会改变标题的字体吗? - Eagle_Eye
在TextView中,您可以使用TypeFace类添加字体。 - droidchef
标题是AlertDialog中的单独视图。只有在您没有使用AlertDialog的标题并且改为在自定义视图中创建标题时,此方法才有效。这是您上面想要表达的吗?可能需要提及一下。否则,我不知道这怎么能行。 - stuckj

2

不要设置alertdialog的文本,应该从你的布局中设置一个自定义视图。在这样做之前,修改你视图的字体。 尝试这个例子

TextView content = new TextView(this);
     content.setText("on another font");
     content.setTypeface(Typeface.SANS_SERIF);

//如果您正在使用XML生成的视图,请使用第一个示例

 AlertDialog.Builder myalert = new AlertDialog.Builder(this);
     myalert.setTitle("Your title");
     myalert.setView(content);
     myalert.setNeutralButton("Close dialog", null);
     myalert.setCancelable(true);
     myalert.show();

XML 代码...在 XML 中更换您的字体

<TextView
    android:id="@+id/yourid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:text="your content" />

1
你也可以尝试使用Spannable。
            SpannableString s = new SpannableString("My App");
            s.setSpan(new TypefaceSpan(this, "font.otf"), 0, s.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            s.setSpan(new RelativeSizeSpan(1.3f), 0,s.length(), 0);
            builder.setTitle(s)

1

Include this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView
   android:id="@+id/text"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Hello_World"
   android:textColorLink="#FF00FF"
  />

</LinearLayout>

然后在您的Activity中使用它
Dialog dialog = new Dialog(ActivityName.this);
dialog.setContentView(R.layout.dialog_name);
dialog.setCancelable(true);
dialog.findViewById(R.id.text).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));

另一种方法这个

这让我感到困惑。一个简单的问题。我能否在标题的位置设置Textview? - Eagle_Eye
哪一部分让您感到困惑?使用这个会有什么问题吗? - Sam
Sam,我之前很困惑,我们能否使用TextView来设置警告框的标题,而不是使用builder.setTitle("Title")..! 但现在我使用了setCustomTitle()代替setTitle(),并将textview对象作为参数传递。现在我可以通过更改Textview的字体来更改标题的字体。非常感谢你的帮助! - Eagle_Eye

0

你可以添加这个

TextView textViewt = (TextView) alertDialog.findViewById(android.R.id.title);
textViewt.setTypeface(your font typeface);

在这行之后

alertDialog.show();

2020年无效。 - Sam Chen

0

有一种简单的方法可以将新的自定义视图设置为对话框的标题。我们可以定义每个自定义视图,例如TextView,并添加一些自定义属性,最后将其设置为对话框的标题,如下所示:

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



        TextView title_of_dialog = new TextView(getApplicationContext());
        title_of_dialog.setHeight(50);
        title_of_dialog.setBackgroundColor(Color.RED);
        title_of_dialog.setText("Custom title");
        title_of_dialog.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        title_of_dialog.setTextColor(Color.WHITE);
        title_of_dialog.setGravity(Gravity.CENTER);

        builder.setCustomTitle(title_of_dialog);
        builder.create().show();

在这里,我定义了一个动态的TextView并设置了一些属性。最后,我使用“setCustomTitle()”函数将其设置为对话框的标题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接