Android AlertDialog标题字体

Android AlertDialog标题字体,android,android-alertdialog,Android,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

我正在尝试更改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

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


谢谢。

您的问题已在此处得到解答:


您只需使用textview并将其设置为自定义标题,如下所示:
builder.setCustomTitle(tv2)

创建一个简单的文本视图

TextView tv;
替换

builder.setTitle("My Title");


我用这个让它工作:

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);
  }
}
用这个

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

没有任何自定义标题:)

我看到了他的解决方案。这类似于使用自定义布局。有没有办法获得默认的标题文本视图?我认为没有办法获得默认的标题文本视图,但是这个解决方案有什么问题呢。您可以在此设置标题,也可以更改字体。您也可以尝试一些第三方对话库,可能这是最后一个选项。它不会引发Null poiner异常吗?上面的textview(tv)是您的小部件。所以在使用它之前,您需要使用findViewById()方法找到它。我认为您的上下文是空的。检查一下!发布AlertDialog的完整代码。
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);
  }
}
TextView title = (TextView) dialog.findViewById(R.id.alertTitle);