【问题标题】:Change font of text next to radio button?更改单选按钮旁边的文本字体?
【发布时间】:2021-09-29 13:31:00
【问题描述】:

更改我使用的 textView 的字体

TextView tv = (TextView) findViewById(R.id.textview);
Typeface font = Typeface.createFromAsset(getAssets(), "SF_Cartoonist_Hand_Bold.ttf");
tv.setTypeface(font);

我想为单选按钮旁边的文本做类似的事情,我该怎么做?

【问题讨论】:

  • 不也一样吗? RadioButton 有一个 setTypeface() 方法。

标签: android fonts radio-button


【解决方案1】:

您将字体设置为RadioButton 旁边的文本,就像设置TextView 一样:

RadioButton rb  = (RadioButton) findViewById(R.id.radiobutton);
Typeface font = Typeface.createFromAsset(getAssets(), "SF_Cartoonist_Hand_Bold.ttf");
rb.setTypeface(font);

【讨论】:

    【解决方案2】:

    From the official doc:

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

    【讨论】:

    • 调用所需的 api 26。小于 26 怎么办?
    【解决方案3】:

    我个人喜欢在片段或活动之外处理样式。下面的 CustomRadioButton 类应该会处理它。

    XML 布局:

    <com.company.package.ui.CustomRadioButton
        android:id="@+id/radioBtnAge"
        style="@style/RadioButton.One"
        app:fontName="SF-Pro-Text-Medium.otf"/>
    

    CustomRadioButton 类:

    package com.company.package.ui;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    
    public class CustomRadioButton extends android.support.v7.widget.AppCompatRadioButton {
        public CustomRadioButton(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(attrs);
        }
    
        public CustomRadioButton(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(attrs);
    
        }
    
        public CustomRadioButton(Context context) {
            super(context);
            init(null);
        }
    
    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
            String fontName = a.getString(R.styleable.CustomTextView_fontName);
    
            if (fontName == null || fontName.isEmpty()) {
                fontName = FontCacheHelper.DEFAULT_FONT_NAME;
            }
    
            Typeface myTypeface = FontCacheHelper.getInstance().getAppFont(getContext(), fontName);
            setTypeface(myTypeface);
            a.recycle();
        }
    }
    }
    

    FontCacheHelper 高效管理字体(参考:https://slothdevelopers.wordpress.com/2014/05/11/custom-fonts-in-textview-and-fontcache/

    package com.company.package.util;
    
    import android.content.Context;
    import android.graphics.Typeface;
    import android.support.annotation.NonNull;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class FontCacheHelper {
        private static final String FEATURE_FONT_PATH = "Fonts/";
        private static final String APP_FONT_PATH = "Fonts/App/";
        public static final String DEFAULT_FONT_NAME = "SF-Pro-Text-Medium.otf";
        private static FontCacheHelper fontCacheHelper;
        private Map<String, Typeface> fontMap = new HashMap<>();
    
        public static FontCacheHelper getInstance() {
            if (fontCacheHelper == null) {
                fontCacheHelper = new FontCacheHelper();
            }
            return fontCacheHelper;
        }
    
        public
        @NonNull
        Typeface getAppFont(@NonNull Context context, @NonNull String fontName) {
            String fontPath = getAppFontFilePath(fontName); 
            return getFontByPath(context, fontPath);
        }
    
        public
        @NonNull
        Typeface getFeatureFont(@NonNull Context context, @NonNull String fontName) {
            String fontPath = getFeatureFontFilePath(fontName);
            return getFontByPath(context, fontPath);
        }
    
        public
        @NonNull
        Typeface getFontByPath(@NonNull Context context, @NonNull String fontPath) {
            if (fontMap.containsKey(fontPath)) {
                return fontMap.get(fontPath);
            } else {
                Typeface typeface;
                if (fontPath.isEmpty()) {
                    typeface = Typeface.DEFAULT;
                } else {
                    typeface = Typeface.createFromAsset(context.getAssets(), fontPath);
                    if (typeface == null) {
                        typeface = Typeface.DEFAULT;
                    }
                }
                fontMap.put(fontPath, typeface);
                return typeface;
            }
        }
    
        public
        @NonNull
        String getDefaultFontFilePath() {
            return APP_FONT_PATH + DEFAULT_FONT_NAME;
        }
    
        private
        @NonNull
        String getAppFontFilePath(@NonNull String fontName) {
            return APP_FONT_PATH + fontName;
        }
    
        private
        @NonNull
        String getFeatureFontFilePath(@NonNull String fontName) {
            return FEATURE_FONT_PATH + fontName;
        }
    
        public
        @NonNull
        String getFontName(@NonNull Typeface typeface) {
            for (Map.Entry<String, Typeface> entry : fontMap.entrySet()) {
                if (entry.getValue().equals(typeface)) {
                    return entry.getKey();
                }
            }
            return "";
        }
    }
    

    【讨论】:

      【解决方案4】:

      在 kotlin 中,您可以通过编程方式更改单选按钮的字体

      val typeface =ResourcesCompat.getFont(context, R.font.myfont) 
      radioButton.typeface= typeface
      

      【讨论】:

        【解决方案5】:

        正如我在这个答案中所描述的: https://stackoverflow.com/a/65262349/2802042

        你可以使用这条线:

          app:fontFamily="@font/myFontFamily"
        

        【讨论】:

          猜你喜欢
          • 2022-01-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-25
          • 1970-01-01
          • 2021-11-15
          • 1970-01-01
          相关资源
          最近更新 更多