首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将typeFace设置为NumberPicker

将typeFace设置为NumberPicker
EN

Stack Overflow用户
提问于 2014-09-12 02:27:25
回答 6查看 7.8K关注 0票数 14

如何在NumberPicker中更改字体类型。我试着这样做,但字体没有改变。有什么想法吗?附言:颜色和textSize是变化的。

代码语言:javascript
复制
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);
         }
       }

     }

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

EN

回答 6

Stack Overflow用户

发布于 2020-04-21 21:59:59

在你的style.xml中添加下面的样式。

代码语言:javascript
复制
<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中的Number Picker。

代码语言:javascript
复制
android:theme="@style/NumberPickerCustomText"
票数 4
EN

Stack Overflow用户

发布于 2015-10-26 15:20:14

我通过在addView中设置Typeface数据来解决同样的问题,如下所示:

代码语言:javascript
复制
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));
    }

}

}
票数 3
EN

Stack Overflow用户

发布于 2014-09-12 02:45:01

updateView(View view)方法中

解决方案1

代码语言:javascript
复制
private void updateView(View view) {
     if(view instanceof EditText){
       ((EditText) view).setTypeface(Typeface.SERIF);
       ((EditText) view).setTextSize(25);
       ((EditText) view).setTextColor(Color.RED);
     }

有关其他字体的信息,请参阅here

解决方案2

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

代码语言:javascript
复制
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/yours_font.ttf"); 
((EditText) view).setTypeface(type);

解决方案3

参考this SO question获得一个很好的方法来实现你的want.Hope帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25794611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档