【问题标题】:Spinner with custom text font and color具有自定义文本字体和颜色的微调器
【发布时间】:2019-02-07 07:09:49
【问题描述】:

我想在 Spinner 中使用自定义字体和颜色。我不能直接在<spinner/>修改它们

这是我的代码

<Spinner
                android:id="@+id/spinner2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="5pt"
                android:layout_marginTop="5pt"
                android:backgroundTint="#d9d9d9"
                android:entries="@array/dropdown_main" />

应该是这样的:

文本字体为 Product Sans Bold,颜色为 #333333

【问题讨论】:

    标签: android kotlin spinner


    【解决方案1】:

    在这里,您可以在布局文件夹中创建一个自定义 xml 文件,您可以在其中添加:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#333333"
    android:padding="10dp"
    android:textStyle="bold"  /> 
    

    然后在您的代码中这样提及:

    val adapter = ArrayAdapter.createFromResource(this, R.array.array_name, R.layout.custom_spinner) // where array_name consists of the items to show in Spinner
    adapter.setDropDownViewResource(R.layout.custom_spinner) // where custom-spinner is mycustom xml file. 
    

    然后设置适配器。

    【讨论】:

      【解决方案2】:

      开始之前的注意事项:要添加字体,您需要将最低 API 版本设置为 26 或包含支持库 v26.0(用于从 API 版本 16 开始的支持)。这个例子展示了如何使用支持库;唯一真正的区别是&lt;font-family&gt; 使用appres-auto 命名空间而不是android

      您可以保持微调器不变,但在您的 XML 中添加 theme 值:

      <Spinner
          android:id="@+id/spinner2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginStart="5pt"
          android:layout_marginTop="5pt"
          android:backgroundTint="#d9d9d9"
          android:entries="@array/dropdown_main"
          android:theme="@style/SpinnerTheme"  />
      

      您的styles.xml 可以包含主题:

      <style name="SpinnerTheme">
          <item name="android:textColor">#333333</item>
          <item name="android:fontFamily">@font/product_sans</item>
          <item name="android:textStyle">bold</item>
      </style>
      

      要添加字体,您需要做一些事情:

      1. 添加字体文件夹:右键单击您的res 文件夹并选择新建> Android 资源目录。确保选择“字体”的资源类型(也可能选择名称。)
      2. 将 Product Sans 字体文件从 https://befonts.com/product-sans-font.html 之类的地方添加到您的项目中。
      3. 右击res下的font文件夹并选择新建>字体资源文件。将文件命名为product_sans.xml
      4. 列出您添加的字体:

      如果您使用支持库,请确保在此处添加 app 命名空间。否则,如果您使用的是 SDK 版本 26 或更高版本,则可以引用 android 命名空间。

      <font-family xmlns:app="http://schemas.android.com/apk/res-auto">
          <font app:font="@font/product_sans_regular" app:fontWeight="400" app:fontStyle="normal" />
          <font app:font="@font/product_sans_italic" app:fontWeight="400" app:fontStyle="italic" />
          <font app:font="@font/product_sans_bold" app:fontWeight="700" app:fontStyle="normal" />
          <font app:font="@font/product_sans_bold_italic" app:fontWeight="700" app:fontStyle="italic" />
      </font-family>
      

      可以在此处找到有关 XML 中字体的更多信息:https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

      【讨论】:

      • 完美解决方案!
      • android:fontFamily 从 API 级别 16 开始支持,而不是 26。
      • @AleksN。 - 正确的!它需要我提到的支持库,但从不调用那里支持的 API 级别。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多