更改警报对话框Android的标题字体

Eag*_*Eye 22 fonts android android-alertdialog

我想更改警报对话框标题字体 ..

任何人都可以告诉我该怎么做...?

任何帮助,将不胜感激..

Eag*_*Eye 28

我找到了一个简单的解

起初我正在设置我的警报框的标题

builder.setTitle("My Title");
Run Code Online (Sandbox Code Playgroud)

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

然后对我有用的是......

我创建了一个简单的TextView:

TextView tv2;
Run Code Online (Sandbox Code Playgroud)

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

然后我替换了我的

builder.setTitle("My Title");
Run Code Online (Sandbox Code Playgroud)

与...一致

builder.setCustomTitle(tv2);
Run Code Online (Sandbox Code Playgroud)

现在我可以改变Title Color,Font等等改变tv2's属性..

  • 那么,按钮怎么样?我怎么能改变它们的颜色或按钮呢? (2认同)

Jar*_*ler 16

没有答案提供了一种改变标题字体的方法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);
  }

}
Run Code Online (Sandbox Code Playgroud)

添加以下实用程序方法:

/**
 * <p>Return spannable string with applied typeface in certain style</p>
 *
 * http://stackoverflow.com/questions/8607707/how-to-set-a-custom-font-in-the-actionbar-title
 *
 * @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;
}
Run Code Online (Sandbox Code Playgroud)

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

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


Moh*_*eza 8

只需使用以下行来获取对话框标题的标识符:

int dialogTitle = mCtnx.getResources().getIdentifier( "alertTitle", "id", "android" );
Run Code Online (Sandbox Code Playgroud)

将此项用于android.support.v7.app.AlertDialog

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