我在小部件中的自定义字体有问题。我尝试了这个问题的方法。我将字体文件(Moon.ttf)放在资产/字体中。在值/属性中添加一个 xml。当我将小部件添加到主屏幕时,它会显示加载问题。在 logcat 中显示了这些。我不知道为什么它不起作用。
09-10 19:26:14.914: W/AppWidgetHostView(2535): updateAppWidget couldn't find any view, using error view
09-10 19:26:14.914: W/AppWidgetHostView(2535): android.view.InflateException: Binary XML file line #40: Error inflating class com.pxr.tutorials.widget.basic.FontableTextView
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.widget.RemoteViews.apply(RemoteViews.java:930)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:219)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:250)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:73)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.os.Looper.loop(Looper.java:123)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.app.ActivityThread.main(ActivityThread.java:4641)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at java.lang.reflect.Method.invokeNative(Native Method)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at java.lang.reflect.Method.invoke(Method.java:521)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at dalvik.system.NativeStart.main(Native Method)
09-10 19:26:14.914: W/AppWidgetHostView(2535): Caused by: java.lang.ClassNotFoundException: com.pxr.tutorials.widget.basic.FontableTextView in loader dalvik.system.PathClassLoader[.]
09-10 19:26:14.914: W/AppWidgetHostView(2535): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
09-10 19:26:14.914: W/AppWidgetHostView(2535): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
这是 FontableTextView 类:
public class FontableTextView extends TextView {
public static final String TAG = "TextView";
public FontableTextView(Context context) {
super(context);
}
public FontableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context,attrs);
}
public FontableTextView(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.FontableTextView);
String customFont = a.getString(R.styleable.FontableTextView_font);
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 false;
}
}
attrs.xml
<resources>
<declare-styleable name="FontableTextView">
<attr name="font" format="string"/>
</declare-styleable>
</resources>
和小部件布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.pxr.tutorials.widget.basic"
android:id="@+id/LinearLayout01"
android:layout_height="200dp"
android:layout_width="320dp"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:src="@drawable/quote1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
<TextView
android:paddingTop="20dp"
android:id="@+id/widget_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textColor="@android:color/black"
android:text="@string/quote"
android:textSize="7pt"
android:layout_weight="2"
android:textStyle="italic"
android:ellipsize="end"
/>
<com.pxr.tutorials.widget.basic.FontableTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="5pt"
android:text="@string/app_name"
android:textColor="@android:color/black"
foo:font="Moon.ttf"
></com.pxr.tutorials.widget.basic.FontableTextView>
<TextView
android:id="@+id/widget_textview_author"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom|left"
android:textColor="@android:color/black"
android:text="@string/author"
android:textSize="5pt"
android:paddingLeft="0dp"
android:paddingBottom="25dp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>