简体   繁体   English

自定义枚举属性提供错误不允许使用字符串类型

[英]Custom enum attribute gives error String types not allowed

Hello guys I am trying to implement a custom TextView with typeface. 大家好我正在尝试用字体实现自定义TextView。 I've decided to use RobotoTextView. 我决定使用RobotoTextView。 I have as well fonts folder in the assets. 我在资产中也有字体文件夹。 I've got error on line where i've got **custom:typeface="robotoLight"** . 我有错误在线,我有**custom:typeface="robotoLight"**

The Error: 错误:

AGPBI: {"kind":"error","text":"String types not allowed (at \'typeface\' with value \'robotoLight\').","sources":[{"file":"path/to/file/thisxml.xml","position":{"startLine":32,"startColumn":33,"startOffset":1357,"endColumn":44,"endOffset":1368}}],"original":""} AGPBI:{“kind”:“error”,“text”:“不允许字符串类型(在\\ u0027typeface \\ u0027 with value \\ u0027robotoLight \\ u0027)。”,“sources”:[{“file”:“path / to /file/thisxml.xml","position":{"startLine":32,"startColumn":33,"startOffset":1357,"endColumn":44,"endOffset":1368}}],"original“: “”}

Any help would be appreciated. 任何帮助,将不胜感激。

My layout with custom TextView 我的自定义TextView布局

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/my.package.name"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#99f9fafa"
        android:padding="0dp" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#99f9fafa"
            android:orientation="vertical"
            android:padding="0dp" >
            <ImageView
                android:id="@+id/bigImage"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/noproperty"/>

            <utils.RobotoTextView
                android:id="@+id/propertyPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:text="Product Name"
                android:textColor="@color/main_color_grey_800"
                android:textSize="14sp"
                custom:typeface="robotoLight"/>
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/main_purple"/>
        <!--android:background="@color/main_color_500" />-->
</LinearLayout>

Here is the attribute file with styleable: 这是带有样式的属性文件:

<declare-styleable name="RobotoTextView">
    <attr name="typeface" format="enum">
        <enum name="robotoBlack" value="0" />
        <enum name="robotoBlackItalic" value="1" />
        <enum name="robotoBold" value="2" />
        <enum name="robotoBoldItalic" value="3" />
        <enum name="robotoBoldCondensed" value="4" />
        <enum name="robotoBoldCondensedItalic" value="5" />
        <enum name="robotoCondensed" value="6" />
        <enum name="robotoCondensedItalic" value="7" />
        <enum name="robotoItalic" value="8" />
        <enum name="robotoLight" value="9" />
        <enum name="robotoLightItalic" value="10" />
        <enum name="robotoMedium" value="11" />
        <enum name="robotoMediumItalic" value="12" />
        <enum name="robotoRegular" value="13" />
        <enum name="robotoThin" value="14" />
        <enum name="robotoThinItalic" value="15" />
    </attr>
</declare-styleable>

My class with the Custom Text View public class RobotoTextView extends TextView { 我的自定义文本视图公共类RobotoTextView的类扩展了TextView {

    public RobotoTextView(Context context) {
        super(context);
        if (isInEditMode()) return;
        parseAttributes(null);
    }

    public RobotoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (isInEditMode()) return;
        parseAttributes(attrs);
    }

    public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (isInEditMode()) return;
        parseAttributes(attrs);
    }

    private void parseAttributes(AttributeSet attrs) {
        int typeface;
        if (attrs == null) { //Not created from xml
            typeface = Roboto.ROBOTO_REGULAR;
        } else {
            TypedArray values = getContext().obtainStyledAttributes(attrs, R.styleable.RobotoTextView);
            typeface = values.getInt(R.styleable.RobotoTextView_typeface, Roboto.ROBOTO_REGULAR);
            values.recycle();
        }
        setTypeface(getRoboto(typeface));
    }

    public void setRobotoTypeface(int typeface) {
        setTypeface(getRoboto(typeface));
    }

    private Typeface getRoboto(int typeface) {
        return getRoboto(getContext(), typeface);
    }

    public static Typeface getRoboto(Context context, int typeface) {
        switch (typeface) {
            case Roboto.ROBOTO_BLACK:
                if (Roboto.sRobotoBlack == null) {
                    Roboto.sRobotoBlack = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
                }
                return Roboto.sRobotoBlack;
            case Roboto.ROBOTO_BLACK_ITALIC:
                if (Roboto.sRobotoBlackItalic == null) {
                    Roboto.sRobotoBlackItalic = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BlackItalic.ttf");
                }
                return Roboto.sRobotoBlackItalic;
            case Roboto.ROBOTO_BOLD:
                if (Roboto.sRobotoBold == null) {
                    Roboto.sRobotoBold = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
                }
                return Roboto.sRobotoBold;
            case Roboto.ROBOTO_BOLD_CONDENSED:
                if (Roboto.sRobotoBoldCondensed == null) {
                    Roboto.sRobotoBoldCondensed = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensed.ttf");
                }
                return Roboto.sRobotoBoldCondensed;
            case Roboto.ROBOTO_BOLD_CONDENSED_ITALIC:
                if (Roboto.sRobotoBoldCondensedItalic == null) {
                    Roboto.sRobotoBoldCondensedItalic = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf");
                }
                return Roboto.sRobotoBoldCondensedItalic;
            case Roboto.ROBOTO_BOLD_ITALIC:
                if (Roboto.sRobotoBoldItalic == null) {
                    Roboto.sRobotoBoldItalic = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldItalic.ttf");
                }
                return Roboto.sRobotoBoldItalic;
            case Roboto.ROBOTO_CONDENSED:
                if (Roboto.sRobotoCondensed == null) {
                    Roboto.sRobotoCondensed = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Condensed.ttf");
                }
                return Roboto.sRobotoCondensed;
            case Roboto.ROBOTO_CONDENSED_ITALIC:
                if (Roboto.sRobotoCondensedItalic == null) {
                    Roboto.sRobotoCondensedItalic = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-CondensedItalic.ttf");
                }
                return Roboto.sRobotoCondensedItalic;
            case Roboto.ROBOTO_ITALIC:
                if (Roboto.sRobotoItalic == null) {
                    Roboto.sRobotoItalic = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Italic.ttf");
                }
                return Roboto.sRobotoItalic;
            case Roboto.ROBOTO_LIGHT:
                if (Roboto.sRobotoLight == null) {
                    Roboto.sRobotoLight = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
                }
                return Roboto.sRobotoLight;
            case Roboto.ROBOTO_LIGHT_ITALIC:
                if (Roboto.sRobotoLightItalic == null) {
                    Roboto.sRobotoLightItalic = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-LightItalic.ttf");
                }
                return Roboto.sRobotoLightItalic;
            case Roboto.ROBOTO_MEDIUM:
                if (Roboto.sRobotoMedium == null) {
                    Roboto.sRobotoMedium = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf");
                }
                return Roboto.sRobotoMedium;
            case Roboto.ROBOTO_MEDIUM_ITALIC:
                if (Roboto.sRobotoMediumItalic == null) {
                    Roboto.sRobotoMediumItalic = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-MediumItalic.ttf");
                }
                return Roboto.sRobotoMediumItalic;
            default:
            case Roboto.ROBOTO_REGULAR:
                if (Roboto.sRobotoRegular == null) {
                    Roboto.sRobotoRegular = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
                }
                return Roboto.sRobotoRegular;
            case Roboto.ROBOTO_THIN:
                if (Roboto.sRobotoThin == null) {
                    Roboto.sRobotoThin = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Thin.ttf");
                }
                return Roboto.sRobotoThin;
            case Roboto.ROBOTO_THIN_ITALIC:
                if (Roboto.sRobotoThinItalic == null) {
                    Roboto.sRobotoThinItalic = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-ThinItalic.ttf");
                }
                return Roboto.sRobotoThinItalic;
        }
    }

    public static class Roboto {
        public static final int ROBOTO_BLACK = 0;
        public static final int ROBOTO_BLACK_ITALIC = 1;
        public static final int ROBOTO_BOLD = 2;
        public static final int ROBOTO_BOLD_ITALIC = 3;
        public static final int ROBOTO_BOLD_CONDENSED = 4;
        public static final int ROBOTO_BOLD_CONDENSED_ITALIC = 5;
        public static final int ROBOTO_CONDENSED = 6;
        public static final int ROBOTO_CONDENSED_ITALIC = 7;
        public static final int ROBOTO_ITALIC = 8;
        public static final int ROBOTO_LIGHT = 9;
        public static final int ROBOTO_LIGHT_ITALIC = 10;
        public static final int ROBOTO_MEDIUM = 11;
        public static final int ROBOTO_MEDIUM_ITALIC = 12;
        public static final int ROBOTO_REGULAR = 13;
        public static final int ROBOTO_THIN = 14;
        public static final int ROBOTO_THIN_ITALIC = 15;

        private static Typeface sRobotoBlack;
        private static Typeface sRobotoBlackItalic;
        private static Typeface sRobotoBold;
        private static Typeface sRobotoBoldItalic;
        private static Typeface sRobotoBoldCondensed;
        private static Typeface sRobotoBoldCondensedItalic;
        private static Typeface sRobotoCondensed;
        private static Typeface sRobotoCondensedItalic;
        private static Typeface sRobotoItalic;
        private static Typeface sRobotoLight;
        private static Typeface sRobotoLightItalic;
        private static Typeface sRobotoMedium;
        private static Typeface sRobotoMediumItalic;
        private static Typeface sRobotoRegular;
        private static Typeface sRobotoThin;
        private static Typeface sRobotoThinItalic;
    }
}

Change your following code 更改以下代码

<declare-styleable name="RobotoTextView">
    <attr name="typeface" format="enum">
        <enum name="robotoBlack" value="0" />
        <enum name="robotoBlackItalic" value="1" />
        <enum name="robotoBold" value="2" />
        <enum name="robotoBoldItalic" value="3" />
        <enum name="robotoBoldCondensed" value="4" />
        <enum name="robotoBoldCondensedItalic" value="5" />
        <enum name="robotoCondensed" value="6" />
        <enum name="robotoCondensedItalic" value="7" />
        <enum name="robotoItalic" value="8" />
        <enum name="robotoLight" value="9" />
        <enum name="robotoLightItalic" value="10" />
        <enum name="robotoMedium" value="11" />
        <enum name="robotoMediumItalic" value="12" />
        <enum name="robotoRegular" value="13" />
        <enum name="robotoThin" value="14" />
        <enum name="robotoThinItalic" value="15" />
    </attr>
</declare-styleable>

to this 对此

<declare-styleable name="RobotoTextView"> 
     <attr name="typeface" />
</declare-styleable>

<attr name="typeface" format="enum">
    <enum name="robotoBlack" value="0" />
    <enum name="robotoBlackItalic" value="1" />
    <enum name="robotoBold" value="2" />
    <enum name="robotoBoldItalic" value="3" />
    <enum name="robotoBoldCondensed" value="4" />
    <enum name="robotoBoldCondensedItalic" value="5" />
    <enum name="robotoCondensed" value="6" />
    <enum name="robotoCondensedItalic" value="7" />
    <enum name="robotoItalic" value="8" />
    <enum name="robotoLight" value="9" />
    <enum name="robotoLightItalic" value="10" />
    <enum name="robotoMedium" value="11" />
    <enum name="robotoMediumItalic" value="12" />
    <enum name="robotoRegular" value="13" />
    <enum name="robotoThin" value="14" />
    <enum name="robotoThinItalic" value="15" />
 </attr>
xmlns:custom="http://schemas.android.com/apk/res/my.package.name" 

is the old style of adding a custom namespace. 是添加自定义命名空间的旧样式。 Try using the following which automatically substitutes the correct package for you -- 尝试使用以下内容自动替换正确的包裹 -

xmlns:custom="http://schemas.android.com/apk/res-auto"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:(23,26)不允许使用字符串类型(在&#39;src&#39;中值为&#39;pen&#39;)。 - Error:(23, 26) String types not allowed (at 'src' with value 'pen').? 错误:Android Studio中不允许使用字符串类型(在“主题”中值为“”) - Error : String types not allowed (at 'theme' with value ") in android studio Android动画-不允许使用字符串类型 - Android Animation - String types not allowed 枚举类型可能未实例化错误 - Enum types may not be instantiated error 将枚举反序列化为具有字符串属性的对象 - Deserialize enum into object with string attribute 错误:不允许使用字符串类型(在&#39;layout_height&#39;中,值为&#39;wrap content&#39;)。 activity_main.xml中 - Error: String types not allowed (at 'layout_height' with value 'wrap content'). activity_main.xml layout_toRightOf:不允许的字符串类型 - layout_toRightOf: String types not allowed 错误:(453,69)不允许的字符串类型(在“ activity_horizo​​ntal_margin”处,值为“) - Error:(453, 69) String types not allowed (at 'activity_horizontal_margin' with value '') jaxb枚举到字符串自定义映射 - jaxb Enum to string custom mapping 如何提供枚举字符串作为属性的值? - How to provide enum string as value of an attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM