Android 将字体设置为NumberPicker

Android 将字体设置为NumberPicker,android,typeface,numberpicker,Android,Typeface,Numberpicker,如何在NumberPicker中更改字体类型。我试着这样做,但字体不变。有什么想法吗? 注:颜色和文本大小都在变化 public class NumberPicker extends android.widget.NumberPicker { private Context context; private Typeface tfs; public NumberPicker(Context context, AttributeSet attrs) {

如何在NumberPicker中更改字体类型。我试着这样做,但字体不变。有什么想法吗? 注:颜色和文本大小都在变化

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);
         }
       }

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

在您的
updateView(视图)
方法中

解决方案1

private void updateView(View view) {
     if(view instanceof EditText){
       ((EditText) view).setTypeface(Typeface.SERIF);
       ((EditText) view).setTextSize(25);
       ((EditText) view).setTextColor(Color.RED);
     }
其他字体见

解决方案2

如果您有自己的
.ttf
字体文件,请在assets文件夹上创建一个fonts文件夹,并将
.ttf
字体文件放入
onCreate()
函数中写入:

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/yours_font.ttf"); 
((EditText) view).setTypeface(type);
解决方案3


请参阅一个实现您想要的东西的好方法。希望这有帮助。

我通过在
addView
中设置
字体
数据来解决同样的问题,如下面的代码:

public class CustomNumberPicker extends android.widget.NumberPicker {

Typeface type;

public CustomNumberPicker(Context context, AttributeSet attrs) {
    super(context, attrs);

}

@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);
    type = Typeface.createFromAsset(getContext().getAssets(),
            "fonts/b_yekan.ttf");
    updateView(child);
}

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

    type = Typeface.createFromAsset(getContext().getAssets(),
            "fonts/b_yekan.ttf");
    updateView(child);
}

private void updateView(View view) {

    if (view instanceof EditText) {
        ((EditText) view).setTypeface(type);
        ((EditText) view).setTextSize(25);
        ((EditText) view).setTextColor(getResources().getColor(
                R.color.text_dark_grey));
    }

}

}

这不是一个很好的解决方案,但如果您在updateView中设置字体,它可以工作:-|

private void updateView(View view) {
    Typeface  tfs = Typeface.createFromAsset(context.getAssets(),"fonts/font.ttf");
    if (view instanceof EditText) {
        ((EditText) view).setTypeface(tfs);
        ((EditText) view).setTextSize(25);
        ((EditText) view).setTextColor(getResources().getColor(
            R.color.text_dark_grey));
    }
}

很抱歉反应太晚,但这就是我实现您所期望的目标的方式。我以前使用MaterialNumberPickers而不是stock NumberPickers。github存储库的自述文件中有关于如何添加Gradle依赖项的说明,因此我不在这里介绍它们

然后,我将MaterialNumberPicker的xml字体设置如下:

<com.github.stephenvinouze.materialnumberpickercore.MaterialNumberPicker
        android:id="@+id/np_timer_hour"
        .
        .
        .
        app:mnpFontname="monsterrat_regular.ttf"
        app:mnpTextColor="@color/primaryTextColor"
        app:mnpTextSize="28sp"/>


此外,我还按照说明将monsterrat-regular.ttf放在app>src>main>assets>font下。

在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>

你能看看我的帖子吗?我描述我的班级!正确地说,我描述了如何覆盖默认视图。事实上,我和你写的一样,但是字体不设置Use
getContext()
是的,数字选择器是一个视图组,所以,这就是为什么,首先你需要覆盖扩展android.widget.NumberPicker的自定义类,而不是NumberPicker的外观代码,它嵌入到android中,并将所有组件划分到组中,并在代码中动态查找,而不是从单独的视图组中获取组件。picker->viewGroups->EditText我知道这是很久以前提出的问题,但我只是考虑在我的项目中实现这一点
addView
在构造函数之前被调用,因此,当您尝试设置它时,您的字体为空。谢谢。你救了我:)成功了。谢谢!似乎不应提及父字段
<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>
android:theme="@style/NumberPickerCustomText"