如何在NumberPicker中更改字体类型。我试着这样做,但字体没有改变。有什么想法吗?附言:颜色和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);
}
}
}字体和路径工作正常。我将它用于我的自定义文本视图。
发布于 2020-04-21 21:59:59
在你的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中的Number Picker。
android:theme="@style/NumberPickerCustomText"发布于 2015-10-26 15:20:14
我通过在addView中设置Typeface数据来解决同样的问题,如下所示:
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));
}
}
}发布于 2014-09-12 02:45:01
在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获得一个很好的方法来实现你的want.Hope帮助。
https://stackoverflow.com/questions/25794611
复制相似问题