我正在开发一个 android 项目,该应用程序已经构建,但只是为了改进用户界面。我正在重新设计应用程序。为此,我也尝试使用自定义字体,但这不起作用。我浏览了网上可用的示例代码,但没有一个不能解决我的问题。这是我的做法。
package com.crittermap.backcountrynavigator.dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/handsean.ttf");
setTypeface(tf);
}
}
}
这是XML部分
<com.crittermap.backcountrynavigator.dialog.MyTextView
android:id="@+id/offlinesummary"
android:layout_marginLeft="5dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/offlinecheckbox"
android:text="@string/cp_preview_off_desc"
style="@style/textN_l">
</com.crittermap.backcountrynavigator.dialog.MyTextView>
风格 :
<style name="textN_l" parent="w_h">
<item name="android:textColor">@android:color/black</item>
<item name="android:textSize">12sp</item>
</style>
<style name="w_h"> <!-- Width and Height -->
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
我得到的错误是
06-15 08:35:24.778: E/AndroidRuntime(2304): FATAL EXCEPTION: main
06-15 08:35:24.778: E/AndroidRuntime(2304): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.crittermap.backcountrynavigator.license/com.crittermap.backcountrynavigator.dialog.MapControlPanelDialog}: android.view.InflateException: Binary XML file line #24: Error inflating class com.crittermap.backcountrynavigator.dialog.MyTextView
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.os.Looper.loop(Looper.java:123)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-15 08:35:24.778: E/AndroidRuntime(2304): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 08:35:24.778: E/AndroidRuntime(2304): at java.lang.reflect.Method.invoke(Method.java:507)
06-15 08:35:24.778: E/AndroidRuntime(2304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-15 08:35:24.778: E/AndroidRuntime(2304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-15 08:35:24.778: E/AndroidRuntime(2304): at dalvik.system.NativeStart.main(Native Method)
06-15 08:35:24.778: E/AndroidRuntime(2304): Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class com.crittermap.backcountrynavigator.dialog.MyTextView
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-15 08:35:24.778: E/AndroidRuntime(2304): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.app.Activity.setContentView(Activity.java:1657)
06-15 08:35:24.778: E/AndroidRuntime(2304): at com.crittermap.backcountrynavigator.dialog.MapControlPanelDialog.onCreate(MapControlPanelDialog.java:72)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-15 08:35:24.778: E/AndroidRuntime(2304): ... 11 more
06-15 08:35:24.778: E/AndroidRuntime(2304): Caused by: java.lang.reflect.InvocationTargetException
06-15 08:35:24.778: E/AndroidRuntime(2304): at java.lang.reflect.Constructor.constructNative(Native Method)
06-15 08:35:24.778: E/AndroidRuntime(2304): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
06-15 08:35:24.778: E/AndroidRuntime(2304): ... 22 more
06-15 08:35:24.778: E/AndroidRuntime(2304): Caused by: java.lang.RuntimeException: native typeface cannot be made
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.graphics.Typeface.<init>(Typeface.java:147)
06-15 08:35:24.778: E/AndroidRuntime(2304): at android.graphics.Typeface.createFromAsset(Typeface.java:121)
06-15 08:35:24.778: E/AndroidRuntime(2304): at com.crittermap.backcountrynavigator.dialog.MyTextView.init(MyTextView.java:27)
06-15 08:35:24.778: E/AndroidRuntime(2304): at com.crittermap.backcountrynavigator.dialog.MyTextView.<init>(MyTextView.java:17)
06-15 08:35:24.778: E/AndroidRuntime(2304): ... 25 more
谢谢