【问题标题】:Set specific font in a styles.xml在 styles.xml 中设置特定字体
【发布时间】:2010-04-01 17:46:36
【问题描述】:

我正在为我的 android 应用程序定义样式 XML。我有一些我想使用的 TTF 文件,如何设置字体以使用这些文件作为字体,而不是通用的“sans”、“serif”和“monospace”。谢谢

【问题讨论】:

    标签: xml android fonts


    【解决方案1】:

    您只能通过 Java 代码使用自定义字体,而不是通过布局 XML 或样式/主题——抱歉!

    【讨论】:

    • @SergiCastellsaguéMillán:这个问题是关于使用 TTF 字体的。您链接到的答案是任何 TTF 字体。
    • 因为这是一个旧答案;您是否知道这在以后的版本中是否有任何更改,@CommonsWare?我也希望能够通过在“”文档中定义字体来更改字体。
    • @EinarSundgren:在以后的版本中肯定没有改变,有现货的Android。有各种库试图简化字体的使用(例如 Calligraphy)。
    • @CommonsWare “Android O 引入了一项新功能,即 XML 中的字体,它允许您将字体用作资源。Android O 还提供了一种机制来检索与系统字体相关的信息并提供文件描述符。” developer.android.com/preview/features/working-with-fonts.html
    • 现在可以使用支持库,请查看developer.android.com/preview/features/fonts-in-xml.html
    【解决方案2】:

    TextViewPlus.java:

    package com.example;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.widget.TextView;
    
    public class TextViewPlus extends TextView {
        private static final String TAG = "TextView";
    
        public TextViewPlus(Context context) {
            super(context);
        }
    
        public TextViewPlus(Context context, AttributeSet attrs) {
            super(context, attrs);
            setCustomFont(context, attrs);
        }
    
        public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setCustomFont(context, attrs);
        }
    
        private void setCustomFont(Context ctx, AttributeSet attrs) {
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
            String customFont = a.getString(R.styleable.TextViewPlus_customFont);
            setCustomFont(ctx, customFont);
            a.recycle();
        }
    
        public boolean setCustomFont(Context ctx, String asset) {
            Typeface tf = null;
            try {
            tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
            } catch (Exception e) {
                Log.e(TAG, "Could not get typeface: "+e.getMessage());
                return false;
            }
    
            setTypeface(tf);  
            return true;
        }
    
    }
    

    attrs.xml:(在 res/values 中)

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="TextViewPlus">
            <attr name="customFont" format="string"/>
        </declare-styleable>
    </resources>
    

    ma​​in.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:foo="http://schemas.android.com/apk/res/com.example"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <com.example.TextViewPlus
            android:id="@+id/textViewPlus1"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="@string/showingOffTheNewTypeface"
            foo:customFont="saxmono.ttf">
        </com.example.TextViewPlus>
    </LinearLayout>
    

    您可以将“saxmono.ttf”放在 assets 文件夹中。

    【讨论】:

      【解决方案3】:

      是的,你可以:)

      第 1 步:创建一个文件夹并将其命名为“字体”并将您的 .ttf 文件放入其中。

      第 2 步:转到 style.xml 并执行以下操作:-

      第 3 步:在视图中的任意位置使用样式标签(TextView、TabLayout 等):-

      【讨论】:

      • 在这一切之前,首先,您需要更新以更新以支持库 26 这里是一个链接segunfamisa.com/posts/custom-fonts-with-android-support-library
      • 不幸的是它不是那么简单,一些元素(如警报对话框的标题)显然仍然需要以编程方式设置它。但总的来说,这是最新的方法,是的。只是需要更多的工作。
      【解决方案4】:
      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
              <item name="android:layout_width">fill_parent</item>
              <item name="android:layout_height">wrap_content</item>
              <item name="android:textColor">#00FF00</item>
              <item name="android:typeface">monospace</item>
          </style>
      </resources>
      

      【讨论】:

      • 如果我想使用无衬线字体怎么办?
      • OP 询问“我有一些我想使用的 TTF 文件”。等宽在技术上可能是一种字体,但它对这个问题没有帮助。 monospace 是特殊的内置字体,它不能帮助您实现使用您选择的任何 TTF 文件的目标。
      【解决方案5】:

      在资源文件夹中创建一个字体目录并粘贴您的 ttf 字体文件。然后创建字体资源 XML 并粘贴以下行。

          <?xml version="1.0" encoding="utf-8"?>
      <font-family xmlns:android="http://schemas.android.com/apk/res/android">
          <font
              android:fontStyle="normal"
              android:fontWeight="400"
              android:font="@font/roboto_light" />
          <font
              android:fontStyle="italic"
              android:fontWeight="400"
              android:font="@font/roboto_light_italic" />
      
      </font-family>
      

      现在您可以应用如下字体。还要注意属性'textStyle'

        <TextView
                          android:textStyle="italic"
                          android:fontFamily="@font/family_roboto_light"
                          android:textColor="@color/primaryTextColor"
                          android:textSize="20sp"
                          android:gravity="center"
                          android:id="@+id/textView36"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:text="No Internet connection" />
      
        <TextView
                          android:fontFamily="@font/family_roboto_light"
                          android:textStyle="normal"
                          android:textSize="20sp"
                          android:gravity="center"
                          android:id="@+id/textView37"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:text="No Internet connection" />
      

      如果您想从代码中完成,请使用以下 API,但最低 API 级别为 26

      Typeface typeface = getResources().getFont(R.font. roboto_light_italic);
      textView.setTypeface(typeface);
      

      支持库 26.0 在运行 Android 4.1(API 级别 16)及更高版本的设备上提供对 XML 字体功能的支持。

      Typeface typeface = ResourcesCompat.getFont(context, R.font. roboto_light_italic);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-14
        • 2023-03-21
        • 2018-07-29
        相关资源
        最近更新 更多