加载 - 小部件中的自定义字体

时间:2012-09-10 17:43:51

标签: android android-layout android-widget

我在widget中有自定义字体的问题。我试过这个question的方法。 我把字体文件(Moon.ttf)放在资产/字体中。在values / attrs中添加一个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>  

2 个答案:

答案 0 :(得分:0)

主屏幕小部件尚不支持自定义布局。 如果要使用自定义字体,则需要使用Canvas并绘制文本并将其设置为ImageView

答案 1 :(得分:0)

由于下面这行

,你有错误
xmlns:foo="http://schemas.android.com/apk/res/com.pxr.tutorials.widget.basic"

试试这个     xmlns:foo="http://schemas.android.com/apk/res-auto/com.pxr.tutorials.widget.basic" instead of

这里我将我的代码放在

public class CustomTextView extends TextView {

public CustomTextView(Context context) {
    super(context);
    Typeface face = Typeface.createFromAsset(context.getAssets(),
            "LFAX.TTF");
    this.setTypeface(face);
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    Typeface face = Typeface.createFromAsset(context.getAssets(),
            "LFAX.TTF");
    this.setTypeface(face);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    Typeface face = Typeface.createFromAsset(context.getAssets(),
            "LFAX.TTF");
    this.setTypeface(face);
}

@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);
}

}

我的xml是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto/com.ninespl.medoracab.utils"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_color"
tools:ignore="UnknownIdInLayout,HardcodedText,DuplicateIds" >


<com.ninespl.medoracab.utils.CustomTextView
    android:id="@+id/car_listing_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
     >
</com.ninespl.medoracab.utils.CustomTextView>
相关问题