2012-09-10 53 views
0

我在小部件中的自定義字體有問題。我嘗試了這個方法question。 我把資源/字體中的字體文件(Moon.ttf)。在值/ attrs中添加一個xml。當我將小部件添加到主屏幕時,它會顯示加載問題。在logcat中顯示這些。我不明白爲什麼它不起作用。加載probem - 小部件中的自定義字體

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> 

和Widget佈局:

<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> 
+0

當它是一個主屏幕小部件之外使用請問您查看的工作? – wsanville

+0

hmm logcat顯示此「無法獲得字體:null」,但我不知道爲什麼?字體在正確的文件夾中; / – Dariusz

回答

0

自定義佈局尚未支持主屏幕小部件。 您需要使用Canvas和繪製文本,如果你想使用自定義的字體

0

你已經得到,因爲這低於行錯誤將其設置爲ImageView

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>