【问题标题】:Android custom enum for xml tagxml标签的Android自定义枚举
【发布时间】:2017-07-07 19:23:17
【问题描述】:

无论如何,我想在 xml/java 类的某个地方声明枚举。
很简单

enum myEnum {
    FIRST,
    SECOND
}

或者以某种方式在 xml 中声明它。
然后在 xml 中使用它。

结果我需要什么:

<TextView>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="myEnum/FIRST"
</TextView>

<Button>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="myEnum/SECOND"
</Button>


有这样的可能吗?

PS。我需要一些适用于任何标准视图的东西。

【问题讨论】:

  • 嘿尤金,你找到解决方案了吗?
  • 不,我相信没有标准的解决方案。我将标签与@string 资源一起使用。还可以看到书法的方法,挺有意思的。

标签: android xml enums tags


【解决方案1】:

在 attrs.xml 添加以下内容:

  <declare-styleable name="TextView">
    <attr name="font" format="enum">
        <enum name="light" value="0" />
        <enum name="normal" value="1" />
        <enum name="bold" value="2" />
        <enum name="roboto_light" value="3" />
        <enum name="roboto_regular" value="4" />
    </attr>
</declare-styleable>

在自定义文本视图中使用如下方式:

package com.rxmedicalapp.customviews;


public class TextView extends AppCompatTextView {
public TextView(Context context) {
    super(context);
    init(null);
}

public TextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
}

public TextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
}


private void init(AttributeSet attrs) {
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextView);
        int fontCode = a.getInt(R.styleable.TextView_font,Integer.MAX_VALUE);

        Typeface myTypeface=null;
        switch (fontCode){
            case 0:
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_light.ttf");
                break;

            case 1:
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_regular.ttf");
                break;

            case 2:
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_bold.ttf");
                break;

            case 3:
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/robotolight.ttf");
                break;

            case 4:
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/robotoregular.ttf");
                break;

            default:
                myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/robotoregular.ttf");
                break;



        }

        setTypeface(myTypeface);

        a.recycle();
    }
}

public void setUnderLine(boolean underlined){


}

}

【讨论】:

  • 我知道 styleable,但它适用于自定义视图。我需要的是类似的枚举,它适用于任何标准视图。
  • 它有什么用处,因为它不会被标准视图处理。当我们扩展标准视图时,我们必须在扩展类中手动处理自定义 xml 属性。
  • 例如,我想把这样的标签放到任何视图上,然后用它来定义那个视图的字体。
  • 为此,您必须扩展 TextView 并在 java 中处理您的自定义标签,就像我在上面的答案中更新的那样。
  • 嗯,这是个不错的选择!但是按钮、编辑文本等呢?在这种情况下,我将不得不扩展这些类中的每一个。
【解决方案2】:

简单来说,只需将标签设置为字符串格式,然后当您想将其用作枚举时,然后通过以下方式将其转换为枚举值:

myEnum tag = myEnum.valueOf(view.getTag().toString());

您可以将它用于 switch ..case 或任何您想要的地方。 希望这对其他人有帮助。

【讨论】:

    猜你喜欢
    • 2013-02-20
    • 2021-04-08
    • 2011-08-31
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多