自定义字体在Android小工具 [英] Custom Font on a Android Widget

查看:148
本文介绍了自定义字体在Android小工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建具有自定义字体的字体数字时钟部件。

我从这里 线索

解决方案是通过扩展的TextView,并把自定义字体字样的新属性。这相比于画布和位图的解决方案,然后将它传递给RemoteViews一个很好的解决方案。

我也跟着每一步,
1.创建自定义类CustomFont.java
2. attrs.xml定义样式
3.然后把它main.xml中

但我得到了以下错误

  WARN / AppWidgetHostView(18681):updateAppWidget找不到任何视图,使用错误观点
WARN / AppWidgetHostView(18681):android.view.InflateException:二进制XML文件行#17:错误充气类upper.duper.widget.circular.clock.CustomFont
...
...
...
WARN / AppWidgetHostView(18681):抛出java.lang.ClassNotFoundException:致upper.duper.widget.circular.clock.CustomFont装载机dalvik.system.PathClassLoader

缺什么吗?

下面我附上codeS
这是CustomFont.java

 包upper.duper.widget.circular.clock;进口android.content.Context;
进口android.content.res.TypedArray;
进口android.graphics.Typeface;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.widget.TextView;公共类CustomFont的扩展TextView的{
    私有静态最后弦乐TAG =CustomFont的;公共CustomFont的(上下文的背景下){
    超级(上下文);
}公共CustomFont的(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    setCustomFont(背景下,ATTRS);
}公共CustomFont的(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    setCustomFont(背景下,ATTRS);
}私人无效setCustomFont(上下文CTX,ATTRS的AttributeSet){
    TypedArray A = ctx.obtainStyledAttributes(ATTRS,R.styleable.CustomFont);
    字符串CustomFont的= a.getString(R.styleable.CustomFont_customFont);
    setCustomFont(CTX时,CustomFont);
    a.recycle();
}公共布尔setCustomFont(上下文CTX,弦乐资产){
    字体TF = NULL;
    尝试{
    TF = Typeface.createFromAsset(ctx.getAssets(),资产);
    }赶上(例外五){
        Log.e(TAG,无法获取字体:+ e.getMessage());
        返回false;
    }    setTypeface(TF);
    返回true;
}}

这是我的main.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:updup =htt​​p://schemas.android.com/apk/res/upper.duper.widget.circular.clock
机器人:背景=#00000000
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>< AnalogClock机器人:ID =@ + ID / AnalogClock
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:拨打=@绘制/ widgetdial_white
    机器人:hand_hour =@绘制/ widgethour
    机器人:hand_minute =@绘制/ widgetminute/>
< upper.duper.widget.circular.clock.CustomFont
    机器人:ID =@ + ID / TIME
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字颜色=#000000
    机器人:文字样式=大胆
    机器人:layout_gravity =中心
    机器人:单线=真
    机器人:ellipsize =无
    机器人:TEXTSIZE =12SP
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium
    机器人:填充=2DP
    updup:CustomFont的=ALPNMAIN.TTF/>

和我把我的自定义字体(ALPNMAIN.TTF)资产文件夹已。

这是attr​​s.xml

 <资源>
<申报-设置样式名称=CustomFont的>
    < attr指示NAME =CustomFont的格式=字符串/>
< /申报,设置样式>
< /资源>


现在我觉得这是不可能有应用程序插件的东西定制。
href=\"http://stackoverflow.com/questions/3388296/android-using-custom-view-in-widget\">


解决方案

  

upper.duper.widget.circular.clock.CustomFont


您似乎在尝试使用自定义类的布局应用程序部件。这是不支持的。

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.

这篇关于自定义字体在Android小工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆