Roboto font in my android app

前端 未结 3 788
名媛妹妹
名媛妹妹 2020-12-05 22:05

Hi i am writing an android app. And I want the roboto font in it irrespective of the version of the phone. Is there a way to do it?

Thanks, Rahim.

相关标签:
3条回答
  • 2020-12-05 22:33

    Try this link http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/

    Set the typeface property of the control you are targeting to serif... and for the font file I recommend using TTF, it has worked for me in the past

    Also try these links

    http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html

    Android - Using Custom Font

    0 讨论(0)
  • 2020-12-05 22:38

    To set the font in XML is moderately more effort but has the advantage of being able to preview the font inside the Eclipse ADT‘s graphical layout tab of the XML layout editor. Again, first include your custom font .ttf file in the your application’s assets folder.

    Create a custom textview class:

    public class TypefacedTextView extends TextView
    {
     public TypefacedTextView(Context context, AttributeSet attrs)
     {
      super(context, attrs);
    
       // Typeface.createFromAsset doesn't work in the layout editor. Skipping ...
       if (isInEditMode())
       {
        return;
       }
    
       TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
       String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface);
       styledAttrs.recycle();
    
       if (fontName != null)
        {
         Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
         setTypeface(typeface);
        }
       }
      }
    

    Now to include this custom TypefacedTextView in your XML Layouts simply add your XML namespace attribute below the Android XML namespace attribute:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:your_namespace="http://schemas.android.com/apk/res/com.example.app"
    ... />
    

    And use your TypefacedTextView as you would a normal TextView in XML but with your own custom tag, remembering to set your font:

    <com.example.app.TypefacedTextView
      android:id="@+id/list_item_entry_title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center_vertical"
      android:minHeight="48dp"
      android:textColor="#FF787878"
      your_namespace:typeface="Roboto-Regular.ttf" />
    

    See my blog post for more info: http://polwarthlimited.com/2013/05/android-typefaces-including-a-custom-font/

    0 讨论(0)
  • 2020-12-05 22:44

    Yeah why not, you can get the Roboto font :

    Android ICS typography

    Lets say you want to change the font of a text view :

       Typeface tf = Typeface.createFromAsset(getAssets(),
                "fonts/Roboto-Black.ttf");
        TextView tv = (TextView) findViewById(R.id.FontTextView);
        tv.setTypeface(tf);
    
    0 讨论(0)
提交回复
热议问题