如何更改 CardInputWidget (Stripe) 的 fontFamily
How to change the fontFamily of a CardInputWidget (Stripe)
我正在尝试为 CardInputWidget
设置主题以匹配我的应用程序的其余部分,但我似乎无法将 android:fontFamily="@font/font_name"
项目直接应用于视图或使用样式或主题。
这是com.stripe:stripe-android:5.1.0
根据我的实验,通过 android:theme
属性中使用的样式设置 android:fontFamily
是行不通的。
但是,我可以使用此方法为所有 TextView
children 搜索 CardInputWidget
并在运行时更改它们的字体:
public static void setTypeface(Typeface tf, View v) {
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
}
else if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
setTypeface(tf, vg.getChildAt(i));
}
}
}
调用它非常简单:
Typeface tf = Typeface.create("sans-serif-medium", Typeface.NORMAL);
CardInputWidget card = findViewById(R.id.card);
setTypeface(tf, card);
自定义字体可以使用theme属性theme。我在下面给出了代码快照
// use this in card
<com.stripe.android.view.CardInputWidget
android:id="@+id/card_input_widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/textRegular" <---- use this
/>
<style name="textRegular" parent="android:Widget.TextView">
<item name="android:fontFamily">@font/poppins_regular</item>
</style>
我正在尝试为 CardInputWidget
设置主题以匹配我的应用程序的其余部分,但我似乎无法将 android:fontFamily="@font/font_name"
项目直接应用于视图或使用样式或主题。
这是com.stripe:stripe-android:5.1.0
根据我的实验,通过 android:theme
属性中使用的样式设置 android:fontFamily
是行不通的。
但是,我可以使用此方法为所有 TextView
children 搜索 CardInputWidget
并在运行时更改它们的字体:
public static void setTypeface(Typeface tf, View v) {
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
}
else if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
setTypeface(tf, vg.getChildAt(i));
}
}
}
调用它非常简单:
Typeface tf = Typeface.create("sans-serif-medium", Typeface.NORMAL);
CardInputWidget card = findViewById(R.id.card);
setTypeface(tf, card);
自定义字体可以使用theme属性theme。我在下面给出了代码快照
// use this in card
<com.stripe.android.view.CardInputWidget
android:id="@+id/card_input_widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/textRegular" <---- use this
/>
<style name="textRegular" parent="android:Widget.TextView">
<item name="android:fontFamily">@font/poppins_regular</item>
</style>