自定义文本视图属性出错

时间:2014-12-15 07:13:55

标签: android android-xml android-custom-view android-custom-attributes

我正在使用自定义文本视图,其自定义字体为face

我的自定义视图如下所示。

public class MyTextView extends TextView {

    public MyTextView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setType(context, defStyle);
    }

    public MalayalamTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setType(context, attrs.getAttributeIntValue(
                "http://schemas.android.com/apk/res/android", "textStyle", 0));
    }

    public MyTextView (Context context) {
        super(context);
        setType(context, Typeface.NORMAL);
    }

    private void setType(Context context, int Style) {
        if (Cls_Utils.GetMyFontTypeFace(context) != null) {
            this.setTypeface(Cls_Utils.GetMyFontTypeFace(context), Style);
        }

    }
}

我的问题是,当我在XML中将文本样式赋予“粗体”或“斜体”时,它会在eclipse的图形视图中显示错误。

Exception raised during rendering: For input string: "bold"
Exception details are logged in Window > Show View > Error Log

java.lang.NumberFormatException: For input string: "bold"
    at java.lang.NumberFormatException.forInputString(    at java.lang.Long.parseLong(    at com.android.internal.util.XmlUtils_Delegate.convertValueToInt(XmlUtils_Delegate.java:72)
    at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:88)
    at android.util.BridgeXmlPullAttributes.getAttributeIntValue(BridgeXmlPullAttributes.java:134)
    at com.android.layoutlib.bridge.android.BridgeXmlBlockParser.getAttributeIntValue(BridgeXmlBlockParser.java:436)
    at com.kesariweekly.types.MalayalamTextView.<init>(MyTextView .java:19)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(    at sun.reflect.NativeConstructorAccessorImpl.newInstance(    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(    at java.lang.reflect.Constructor.newInstance(    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:438)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:161)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:806)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:385)

我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/imgArticle"
        android:layout_width="90dp"
        android:layout_height="130dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/mTvTitle"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/ic_not_available" />

    <com.myapp.types.MyTextView

        android:id="@+id/mTvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:text="തലക്കെട്ട്"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <com.myapp.types.MyTextView

        android:id="@+id/mTvAuthor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/mTvTitle"
        android:layout_below="@id/mTvTitle"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="7dp"
        android:layout_toRightOf="@+id/imgArticle"
        android:gravity="right"
        android:paddingRight="10dp"
        android:text="ലേഖകൻ"
        android:textColor="@color/black"
        android:textSize="15sp"
        android:textStyle="italic" />

    <com.myapp.types.MyTextView
        android:id="@+id/mTvSummary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tvReadLink"
        android:layout_below="@id/mTvAuthor"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="7dp"
        android:layout_toRightOf="@id/imgArticle"
        android:clickable="true"
        android:onClick="onClickRead"
        android:text="ചുരുക്കരൂപം" />

    <TextView
        android:id="@+id/tvReadLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/imgArticle"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:clickable="true"
        android:onClick="onClickRead"
        android:text="Read >>"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blue_dark" />

</RelativeLayout>

怎么办?我的代码正常工作,并且在模拟器中正确设置了字体,但唯一的问题是xml图形视图中的此错误消息。

请帮忙。抱歉我的英语不好。

1 个答案:

答案 0 :(得分:1)

如果您只在xml图形视图中收到错误,请将其添加到您的代码

 if(!isInEditMode())
         //use fonts

编辑器只显示默认字体。