Change title font of Alert Dialog box

后端 未结 14 1921
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 10:57

I want to change the font of the title of Alert Dialog box.

Can anybody tell how can I do it?

相关标签:
14条回答
  • 2020-12-16 11:23

    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>
    

    Then use it inside Ur 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"));
    


    Another Way and this

    0 讨论(0)
  • 2020-12-16 11:24

    You can try Spannable as well.

                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)
    
    0 讨论(0)
  • 2020-12-16 11:27

    Use this method from support library 26.

    ResourcesCompat.getFont(context, R.font.<YOUR_FONT>);
    

    Read For more: Source

    0 讨论(0)
  • 2020-12-16 11:28

    Just use the following line to get an identifier to the dialog's title:

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

    use this one for android.support.v7.app.AlertDialog

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

    0 讨论(0)
  • I found a simple solution..

    At first I was setting the title of my alert box by

    builder.setTitle("My Title");
    

    So I was not able to change the font of it..

    Then what worked for me is..

    I created a simple TextView :

    TextView tv2;
    

    And set all properties of TextView which I wanted...

    And then I replaced my

    builder.setTitle("My Title");
    

    line with

    builder.setCustomTitle(tv2);
    

    and now I can change Title Color, Font Etc By Changing tv2's Properties..

    0 讨论(0)
  • 2020-12-16 11:33

    No answers provide a way to change the title typeface of an AlertDialog. Here is what I have done:

    Create the below class:

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

    Add the following utility method:

    /**
     * <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;
    }
    

    Finally, set the typeface when creating your AlertDialog:

    Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/your_font.ttf");
    new AlertDialog.Builder(getActivity())
        .setTitle(FontUtils.typeface(typeface, "The title"))
        /* .. */
        .create();
    
    0 讨论(0)
提交回复
热议问题