A heads up before you get started: To add the font, you'll either want to set a minimum API version of 26 or include the Support Library v26.0 (for support starting at API version 16). This example shows how to use the support library; the only real difference is the <font-family>
using the app
or res-auto
namespace instead of android
.
You can keep the spinner as is but add a theme
value to your XML:
<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" />
Your styles.xml
can contain the theme:
<style name="SpinnerTheme">
<item name="android:textColor">#333333</item>
<item name="android:fontFamily">@font/product_sans</item>
<item name="android:textStyle">bold</item>
</style>
To add the font, you'll need to do a few things:
- Add a font folder: Right-click your
res
folder and choose New > Android resource directory. Make sure you pick a resource type of "Font" (and probably the name as well.)
- Add the Product Sans font file to your project from somewhere like https://befonts.com/product-sans-font.html.
- Right-click the
font
folder under res
and choose New > Font resource file. Name the file product_sans.xml
.
- List your added fonts:
Make sure you add the app
namespace here if you're using the support library. Otherwise, if you're at SDK Version 26 or above, you can reference the android
namespace.
<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>
More info about fonts in your XML can be found here: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…