0

我正在开发一个 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

谢谢

4

5 回答 5

2

你忘记命名空间了吗?

<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"
        android:style="@style/textN_l"> 
</com.crittermap.backcountrynavigator.dialog.MyTextView>
于 2012-06-26T10:01:52.763 回答
0

将您的代码更改为

public class MyTextView extends TextView {
 private Paint mPaint;  
float middleY=1;  
float mX=30;  
float DY=30; 
    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");
        mPaint = new Paint();  
        mPaint.setAntiAlias(true);  
        mPaint.setTextSize(16);  
        mPaint.setColor(Color.BLACK);  
        mPaint.setTypeface(tf); 
          //  setTypeface(tf);
        }
    }
    @Override  
    protected void onDraw(Canvas canvas) {  
        // TODO Auto-generated method stub  
        super.onDraw(canvas);  
        canvas.drawColor(0xEFeffff);  
        Paint p = mPaint;   
        p.setTextAlign(Paint.Align.CENTER);          
        canvas.drawText("TESTEST", mX, middleY, p);    

    } 
于 2012-06-26T10:17:17.957 回答
0

你必须在 xml 中使用,如下所示

  <com.crittermap.backcountrynavigator.dialog.MyTextView
        android:id="@+id/offlinesummary"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"   
        android:layout_marginLeft="5dip"          
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/offlinecheckbox"
        android:text="@string/cp_preview_off_desc"
        >
  </com.crittermap.backcountrynavigator.dialog.MyTextView>

在您的任何活动中,您都必须使用这种方式

MyTextView textview=(MyTextView)findViewById(R.id.offlinesummary);
于 2012-06-26T10:35:28.883 回答
0

尝试添加布局宽度和高度。然后,执行程序。

于 2012-06-26T10:17:06.493 回答
0

尝试这个

<com.crittermap.backcountrynavigator.dialog.MyTextView
    android:id="@+id/offlinesummary"
    android:layout_marginLeft="5dip" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"         
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@id/offlinecheckbox"
    android:text="@string/cp_preview_off_desc"
    android:style="@style/textN_l"> 

更新

所以其他代码看起来很完美。唯一的问题是您的“.ttf”文件,可能是该文件已损坏。从 developer.android.com/design/downloads/index.html 下载字体并使用它们。希望它会奏效。

于 2012-06-26T10:26:18.777 回答