简体   繁体   English

Android小部件上的自定义字体

[英]Custom Font on a Android Widget

I'm trying to create a digital clock widget with custom typeface font. 我正在尝试使用自定义字体创建数字时钟小部件。

I got clues from here 我从这里得到线索

Solution was by extending TextView and put new attribute for custom font typeface. 解决方案是通过扩展TextView并为自定义字体字体添加新属性。 This is a good solution compared to drawing Canvas and Bitmap solution then pass it to RemoteViews. 与绘制Canvas和Bitmap解决方案并将其传递给RemoteViews相比,这是一个很好的解决方案。

I followed every step, 1. create custom class CustomFont.java 2. define style in attrs.xml 3. then put it in main.xml 我遵循了每个步骤,1.创建自定义类CustomFont.java 2.在attrs.xml中定义样式3.然后将其放入main.xml中

but I got the following errors 但是我遇到了以下错误

WARN/AppWidgetHostView(18681): updateAppWidget couldn't find any view, using error view
WARN/AppWidgetHostView(18681): android.view.InflateException: Binary XML file line #17: Error inflating class upper.duper.widget.circular.clock.CustomFont
...
...
...
WARN/AppWidgetHostView(18681): Caused by: java.lang.ClassNotFoundException: upper.duper.widget.circular.clock.CustomFont in loader dalvik.system.PathClassLoader

Something missing? 缺少什么?

Here I attach the codes This is CustomFont.java 在这里我附上代码这是CustomFont.java

package upper.duper.widget.circular.clock;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class CustomFont extends TextView {
    private static final String TAG = "CustomFont";

public CustomFont(Context context) {
    super(context);
}

public CustomFont(Context context, AttributeSet attrs) {
    super(context, attrs);
    setCustomFont(context, attrs);
}

public CustomFont(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setCustomFont(context, attrs);
}

private void setCustomFont(Context ctx, AttributeSet attrs) {
    TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomFont);
    String customFont = a.getString(R.styleable.CustomFont_customFont);
    setCustomFont(ctx, customFont);
    a.recycle();
}

public boolean setCustomFont(Context ctx, String asset) {
    Typeface tf = null;
    try {
    tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
    } catch (Exception e) {
        Log.e(TAG, "Could not get typeface: "+e.getMessage());
        return false;
    }

    setTypeface(tf);  
    return true;
}

}

And this is my main.xml 这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:updup="http://schemas.android.com/apk/res/upper.duper.widget.circular.clock"
android:background="#00000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<AnalogClock android:id="@+id/AnalogClock" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:dial="@drawable/widgetdial_white" 
    android:hand_hour="@drawable/widgethour" 
    android:hand_minute="@drawable/widgetminute"/>


<upper.duper.widget.circular.clock.CustomFont
    android:id="@+id/TIME"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textStyle="bold"
    android:layout_gravity="center"
    android:singleLine="true"
    android:ellipsize="none"
    android:textSize="12sp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:padding="2dp"
    updup:customFont="ALPNMAIN.TTF" /> 

And I put my custom font (ALPNMAIN.TTF) on assets folder already. 我已经将自定义字体(ALPNMAIN.TTF)放在了资产文件夹中。

This is attrs.xml 这是attrs.xml

<resources>
<declare-styleable name="CustomFont">
    <attr name="customFont" format="string"/>
</declare-styleable>
</resources>

Now I feel this is not possible to have something "custom" for app widget. 现在,我感觉这不可能为应用程序小部件提供一些“自定义”功能。 Look here 这里

upper.duper.widget.circular.clock.CustomFont upper.duper.widget.circular.clock.CustomFont

You appear to be attempting to use a custom class in a layout for an app widget. 您似乎正在尝试在应用小部件的布局中使用自定义类。 This is not supported. 不支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM