我正在开发一个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
谢谢
答案 0 :(得分: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>
答案 1 :(得分:0)
尝试添加布局宽度和高度。然后,执行程序。
答案 2 :(得分: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);
}
答案 3 :(得分: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并使用它们。 希望它能奏效。
答案 4 :(得分: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);