将typeFace设置为NumberPicker

问题描述 投票:10回答:5

如何在NumberPicker中更改字体类型。我尝试这样做,但字体不变。任何的想法? P.S:color和textSize是变化的。

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

     }

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

android typeface numberpicker
5个回答
3
投票

我通过在Typeface中设置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));
    }

}

}

0
投票

在你的updateView(View view)方法

解决方案1

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()函数内写:

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/yours_font.ttf"); 
((EditText) view).setTypeface(type);

解决方案3

请参考this SO question获得实现目标的绝佳方式。希望这会有所帮助。


0
投票

它不是非常简洁的解决方案,但是如果你在updateView中设置TypeFace,它的工作原理是: - |

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

0
投票

对于迟到的回应感到抱歉,但这就是我实现你所寻求的目标。我使用this来使用MaterialNumberPickers而不是库存NumberPickers。该github存储库的自述文件中有关于如何添加Gradle依赖关系的说明,因此我不会在此处包含它们。

然后我在xml中设置MaterialNumberPicker的字体,如下所示:

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

另外我按照指示here在app> src> main> assets> fonts下放置了monsterrat-regular.ttf。


0
投票
package com.harman.settings.dateandtime;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.harman.settings.R;

public class XNumberPicker extends android.widget.NumberPicker {
    public XNumberPicker(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);
        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) {
            Typeface typeface = null;
            try {
                typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + getContext().getString(R.string.lato_bold));
            } catch (Exception e) {
                Log.e("XNumberPicker", "Unable to load typeface: " + e.getMessage());
            }
            if (typeface != null) {
                ((EditText) view).setTypeface(typeface);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.