将 typeFace 设置为 NumberPicker

2024-01-26

如何更改 NumberPicker 中的字体类型。我尝试这样做,但字体没有改变。任何想法? P.S:颜色和文字大小发生变化。

public class NumberPicker extends android.widget.NumberPicker {
        private Context context;
        private Typeface tfs;
       public NumberPicker(Context context, AttributeSet attrs) {
         super(context, attrs);
         this.context = context;
         tfs = Typeface.createFromAsset(context.getAssets(),"fonts/font.ttf");
       }

       @Override
       public void addView(View child) {
         super.addView(child);
         updateView(child);
       }

       @Override
       public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
         super.addView(child, index, params);
         updateView(child);
       }

       @Override
       public void addView(View child, android.view.ViewGroup.LayoutParams params) {
         super.addView(child, params);
         updateView(child);
       }

       private void updateView(View view) {
         if(view instanceof EditText){
           ((EditText) view).setTypeface(tfs);
           ((EditText) view).setTextSize(25);
           ((EditText) view).setTextColor(Color.RED);
         }
       }

     }

字体和路径工作正常。我将它用于我的自定义文本视图。


在 style.xml 中添加以下样式。

<style name="NumberPickerCustomText">
    <item name="android:textSize">22sp</item> // add if you want to change size
    <item name="android:fontFamily">@font/lato_regular</item> // add font (this font folder is present in res folder)
</style>

直接将此样式添加到 XML 格式的数字选择器中。

android:theme="@style/NumberPickerCustomText"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 typeFace 设置为 NumberPicker 的相关文章

随机推荐