【问题标题】:Error with custom text-view attribute自定义文本视图属性错误
【发布时间】:2014-12-15 07:13:54
【问题描述】:

我正在使用自定义字体作为字体的自定义文本视图

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

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 图形视图中的此错误消息。

请帮忙。对不起我的英语不好。

【问题讨论】:

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


    【解决方案1】:

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

     if(!isInEditMode())
             //use fonts
    

    编辑器将只显示默认字体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2017-05-23
      • 2012-09-28
      • 1970-01-01
      • 2021-07-21
      相关资源
      最近更新 更多