Creating A Digital Clock Widget With A Custom Font

前端 未结 3 2010
野趣味
野趣味 2021-01-01 23:49

I\'m trying to create a digital clock widget with a custom font. And this has proven to be the biggest challenge of my Android experience. (Thought it would be as simple as

相关标签:
3条回答
  • 2021-01-02 00:34

    I had the same problem... You have 2 options as I know till now:

    1. The easiest - If you are using API level 16 or higher you can simply add android:fontFamily="sans-serif-thin" attribute for example in your xml file. Works like a charm!

    This is my TextView:

    <TextView
       android:id="@+id/Time"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:ellipsize="none"
       android:fontFamily="sans-serif-thin"
       android:gravity="center_vertical|center_horizontal"
       android:includeFontPadding="false"
       android:singleLine="true"
       android:text="12:01"
       android:textColor="#fff"
       android:textSize="80sp" />
    

    And this is how it looks like:

    enter image description here

    The only disadvantage of this method is that there isn't large selection of system fonts you can use.

    1. The hard way is just to use your own Class, witch extends the TextView object like that and define it instead normal TextView in your xml file. I do not recommend it because it is possible to appear bugs.

    Edit: For updating the widget every minute - you need to start a Service which can update your clock each minute (or second if you want). Check this example too.

    0 讨论(0)
  • 2021-01-02 00:44

    For API 17 or higher to customize fonts, you need to download .ttf file for any fonts you want then create a directory assets/fonts/ then save your fonts

    Typeface fonttype
    fonttype = Typeface.createFromAsset(getActivity().getAssets(), "fonts/robotoregular.ttf");
    textclock.setTypeface(fonttype);
    
    0 讨论(0)
  • 2021-01-02 00:46

    Use this link to get Textclock below API level 17. You could use .setTypeface(Typeface.createFromAsset(getAssets(),fonts/font.ttf")); with it.

    0 讨论(0)
提交回复
热议问题