Android AlertDialog标题字体

时间:2015-12-01 09:44:25

标签: android alertdialog

我正在尝试更改android.support.v7.app.AlertDialog标题文字的字体。

方法1:

   TextView title = (TextView) dialog.findViewById(android.R.id.title); //returns null

方法2:

   final int titleId = context.getResources().getIdentifier("alertTitle", "id", "android");
   TextView title = (TextView) dialog.findViewById(titleId); //Also returns null.

还有其他方法可以获得标题TextView吗?

请注意我不想使用自定义布局。

感谢。

4 个答案:

答案 0 :(得分:4)

我使用此solution

开始工作
    final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);  

    Typeface tf = //get the typeface.
    CustomTFSpan tfSpan = new CustomTFSpan(tf);
    SpannableString spannableString = new SpannableString(title);
    spannableString.setSpan(tfSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    alertBuilder.setTitle(spannableString);

    AlertDialog dialog = alertBuilder.create();
    dialog.show();

<强> CustomTFSpan

public class CustomTFSpan extends TypefaceSpan {

  private Typeface typeface;

  public CustomTFSpan(Typeface typeface) {
    super("");
    this.typeface = typeface;
  }

  @Override
  public void updateDrawState(TextPaint ds) {
    applyTypeFace(ds, typeface);
  }

  @Override
  public void updateMeasureState(TextPaint paint) {
    applyTypeFace(paint, typeface);
  }

  private static void applyTypeFace(Paint paint, Typeface tf) {
    paint.setTypeface(tf);
  }
}

答案 1 :(得分:3)

使用此

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

没有任何自定义标题:)

答案 2 :(得分:0)

您的问题已在此处回答:Change Title Font Of Alert Dialog Box Android

您只需使用textview并将其设置为自定义标题,如下所示:{{1}}

答案 3 :(得分:0)

创建一个简单的TextView

TextView tv;

并替换

builder.setTitle("My Title");

builder.setCustomTitle(tv);