简体   繁体   中英

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. This is a good solution compared to drawing Canvas and Bitmap solution then pass it to RemoteViews.

I followed every step, 1. create custom class CustomFont.java 2. define style in attrs.xml 3. then put it in 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

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

<?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.

This is 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

You appear to be attempting to use a custom class in a layout for an app widget. This is not supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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