使用自定义字体创建数字时钟小部件

时间:2015-07-11 19:14:15

标签: android widget alarmmanager android-appwidget android-5.1.1-lollipop

我尝试使用自定义字体创建数字时钟小部件。事实证明这是我体验Android的最大挑战。 (认为​​它会像tc.setTypeFace("whatever")一样简单,并且可以用它完成)

最好的方式似乎是抓住TextClock并使用ImageView并使用自定义AlarmManager将Bitmap传递给它,以便每秒为视图创建一个新图像。

在进入Bitmap生成之前,我使用本教程Alarm Manager Widget

在一个简单的TextView上练习

我的问题是我无法每秒更新一次。我用 am.setRepeating(AlarmManager.RTC_WAKEUP , System.currentTimeMillis()+1000, 1000, pi);但它仍然只是每分钟左右更新一次(不过每分钟都会更新)。我认为这与最近的操作系统(Kitkat,Lollipop)处理AlarmManagers的方式有关。

我的问题是:

  1. 这是正确的方法吗?如果没有,请解释我应该做什么。

  2. 如何让小部件每秒更新一次?

3 个答案:

答案 0 :(得分:4)

我有同样的问题...... 到目前为止,我有两个选择:

  1. 最简单 - 如果您使用的是16级或更高级别的API,则可以在xml文件中添加android:fontFamily="sans-serif-thin"属性。 像魅力一样!
  2. 这是我的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" />
    

    这就是它的样子:

    enter image description here

    此方法的唯一缺点是您无法选择大量系统字体。

    1. 困难的方法就是使用自己的类,扩展TextView对象,如that,并在xml文件中定义普通的TextView。 我不推荐它,因为它可能会出现错误。
    2. 编辑:每分钟更新一次小部件 - 您需要启动一个Service,它可以每分钟更新您的时钟(如果需要,可以更新时钟)。 也请检查this example

答案 1 :(得分:3)

使用此link将Textclock降至API级别17以下。 你可以使用.setTypeface(Typeface.createFromAsset(getAssets(),fonts / font.ttf&#34;));  用它。

答案 2 :(得分:3)

对于API 17或更高版本来自定义字体,您需要为所需的任何字体下载.ttf文件,然后创建目录assets / fonts /然后保存字体

enter image description here

Typeface fonttype
fonttype = Typeface.createFromAsset(getActivity().getAssets(), "fonts/robotoregular.ttf");
textclock.setTypeface(fonttype);
相关问题