【问题标题】:How to change the fontfamily of a Spinner?如何更改 Spinner 的字体系列?
【发布时间】:2020-05-19 14:52:19
【问题描述】:

我使用 android studio 创建一个 android 应用程序。我的 API 版本高于 16,并且我使用支持库 26。 我在 res->font 下创建了一个字体系列,我将其命名为“cairo_regular.ttf”。 在我的 android 应用程序中,在我的一个界面中,我使用带有下拉样式的 Spinner 来显示所有国家/地区的列表,以下是 xml 代码:

<android.support.v7.widget.AppCompatSpinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="16dp"
            android:prompt="@string/spinner_title" />

我在java类的构造函数之外声明了适配器,即:

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, countryTab);
            country.setAdapter(arrayAdapter);

我还在布局文件夹中制作了自定义 xml 文件,如下所示:

<android.support.v7.widget.AppCompatTextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/text1"
    style="?android:attr/dropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:fontFamily="@font/cairo_regular"
    android:textAppearance="?android:attr/textAppearanceLargePopupMenu" />

我在自定义 xml 文件中添加“android:fontFamily="@font/cairo_regular"” 以更改微调器中国家/地区列表的字体,但字体不会改变。 我想知道如何在我的应用程序中更改 Spinner 的字体家族。

【问题讨论】:

    标签: android android-layout spinner


    【解决方案1】:

    因此,让我们尝试并了解当您尝试填充 Spinner 时编写此代码行时会发生什么。

    [ArrayAdapter<String> spinnerAdapter= new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, dataInsideSpinner); // here dataInsideSpinner is a List<>
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);]
    

    android.R.layout.simple_list_item_1 是 Spinner 的 Item 的布局,或者当它没有被选中时(它通常在屏幕上的样子) android.R.layout.simple_dropdown_list 是当您选择微调器并出现 Dropdown Items 时呈现的布局

    因此,您可以通过 2 种方式自定义您的微调器。 要么通过修改android.R.layout.simple_list_item_1android.R.layout.simple_dropdown_list 来更改布局文件 或通过更改您的 Java 文件!正如下面的一些答案中所述。但这种方法似乎更简单,更轻松!

    当您尝试使用这两个文件时,您的微调器将显示更改(在这些文件中所做的更改)。

    否则,最好的方法是编写 2 个自定义布局文件 1> custom_spinner_list_item 2> Custom_spinner_dropdown_item(点击渲​​染)

    这里是它是如何完成的。 custom_spinner_list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
        style="?attr/spinnerDropDownItemStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:fontFamily="@font/roman"
        android:singleLine="true"
        android:textAlignment="inherit"
        android:textColor="@color/black90opacity"
        android:textSize="14sp">
    
    </TextView>
    

    custom_spinner_dropdown_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
        style="?attr/spinnerDropDownItemStyle"
        android:layout_width="match_parent"
        android:layout_height="?attr/dropdownListPreferredItemHeight"
        android:ellipsize="marquee"
        android:fontFamily="@font/roman"
        android:singleLine="true"
        android:textAlignment="textStart"
        android:textColor="@color/black90opacity"
        android:textSize="14sp">
    
    </TextView>
    

    现在在将数据插入适配器的地方使用这些文件

     ArrayAdapter<String> spinnerAdapter= new ArrayAdapter<>(getApplicationContext(), R.layout.custom_spinner_list_item.xml, dataInsideSpinner); // here dataInsideSpinner is a List<>
        spinnerAdapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item.xml);
    

    应该很好走! :)

    【讨论】:

    • 这是最好的解决方案,非常感谢兄弟
    【解决方案2】:

    我是这样做的:

    FontCache 类:

    import android.content.Context;
    import android.graphics.Typeface;
    import java.util.HashMap;
    
    
    public class FontCache {
    
    private static HashMap<String, Typeface> fontCache = new HashMap<>();
    
    public static Typeface getTypeface(String fontname, Context context) {
        Typeface typeface = fontCache.get(fontname);
    
        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), fontname);
            } catch (Exception e) {
                return null;
            }
    
            fontCache.put(fontname, typeface);
        }
    
        return typeface;
    }
    }
    

    然后创建一个扩展 TextView 的自定义类:

    public class MontserratRegularTextView extends android.support.v7.widget.AppCompatTextView {
    
    public MontserratRegularTextView(Context context) {
        super(context);
    
        applyCustomFont(context);
    }
    
    public MontserratRegularTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        applyCustomFont(context);
    }
    
    public MontserratRegularTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    
        applyCustomFont(context);
    }
    
    private void applyCustomFont(Context context) {
        Typeface customFont = FontCache.getTypeface("fonts/Montserrat-Regular.otf", context);//your font path here
        setTypeface(customFont);
    }
    }
    

    然后将其添加到您正在制作的自定义 xml 文件中,如下所示:

    <com.example.myapplication.customFonts.MontserratRegularTextView
        android:id="@+id/user_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/nav_header_title"
         />
    

    希望对您有所帮助!

    【讨论】:

    • 请问有其他解决方案吗?
    • @MimiSoftware 你到底想达到什么目的?如果可以,请提供示例屏幕截图
    • 我只想将微调器字体家族更改为 cairo_regular android:fontFamily="@font/cairo_regular"
    • @MimiSoftware 很抱歉我以前从未使用过字体系列,但是这个link 可能会有所帮助
    • 无论如何,谢谢,我最近使用这个链接来创建字体家族
    猜你喜欢
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多