【问题标题】:Setting typeFace to NumberPicker将 typeFace 设置为 NumberPicker
【发布时间】:2014-11-05 19:48:44
【问题描述】:

如何在 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.widget.NumberPicker 的类,而不是查看嵌入到 android 并划分的 NumberPicker 的代码所有组件到组中并在代码中动态找到它,而不是从单独的视图组中获取组件。选择器->viewGroups->EditText
  • 我知道很久以前有人问过这个问题,但我只是考虑为我的项目实施这个问题。 addView 在构造函数之前调用,因此,当您尝试设置它时,您的字体为 null。

标签: android typeface numberpicker


【解决方案1】:

在您的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 文件夹上创建一个字体文件夹并放入你的 .ttf 字体文件,然后在 onCreate() 函数中写入:

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

解决方案 3

请参考this SO question,了解实现您想要的绝妙方法。希望这会有所帮助。

【讨论】:

  • 你能看到我的帖子吗?我描述我的班级!我正确地描述了我如何覆盖默认视图。其实我和你写的一样,但是字体没有设置
  • 使用getContext()
【解决方案2】:

我通过在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));
    }

}

}

【讨论】:

  • 谢谢。你救了我:)
【解决方案3】:

这不是很好的解决方案,但如果你在 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));
    }
}

【讨论】:

    【解决方案4】:

    很抱歉回复晚了,但这就是我实现您想要的东西的方式。我使用 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 的指示将 Monsterrat-regular.ttf 放在 app>src>main>assets>fonts 下。

    【讨论】:

      【解决方案5】:
      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);
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案6】:

        在您的 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"
        

        【讨论】:

        • 工作。谢谢!似乎不应该提及父字段
        猜你喜欢
        • 2014-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-08
        • 1970-01-01
        • 2016-12-22
        • 1970-01-01
        相关资源
        最近更新 更多