参考属性枚举值 [英] Reference attribute enum value

查看:219
本文介绍了参考属性枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能引用的枚举的提供的风格参数时价值?
例如,我们可以这样做:

Is it possible to reference an enum value when supplying style parameters?
For example, we can do:

<!-- layouts.xml -->
<TextView
    android:text="@string/str" />

<!-- values.xml -->
<item type="string" name="str">hi</item>

这正常工作与字符串,整数,尺寸等等,但我需要引用的枚举的价值 - 例如能见度属性。我在寻找这样的:

This works fine with strings, integers, dimensions and so on but I need to reference an enum value - for example the visibility attribute. I'm looking for something like:

<!-- layouts.xml -->
<TextView
    android:visibility="@????/viz" />

<!-- values.xml -->
<item type="?????" name="viz">gone</item>

能见度仅仅是一个例子 - 它可以是任何其他枚举基于属性

visibility is only an example - it could be any other enum based attribute.

我已经发现迄今唯一的解决方法是使用的的样式

The only workaround I have found so far is by using styles

<!-- layouts.xml -->
<TextView
    android:style="@styles/theStyle" />

<!-- styles.xml -->
<style name="theStyle">
    <item name="android:visibility">gone</item>
</style>

但是,这是某种限制,因为它变成,如果你需要使用越来越复杂的真正的风格与TextViews这的特别的风格,仅用于控制的单一属性

But this is somehow restrictive because it becomes increasingly complicated if you need to use real styles with the TextViews and this special style which is only used to control a single attribute.

另一种选择是,如果有一种方法可以参考个人风格的物品。是这样的:

Another option would be if there is a way to reference individual style items. Something like:

<TextView
    android:visibility="@styles/theStyle/visibility" />

不过,我认为上面是更不可能成为可能。

But I think that the above is even less likely to be possible.

推荐答案

每个枚举实际上是一组命名为整数。 例如,能见度属性声明如下:

Each enum is actually a set of named integers. For example, visibility attribute is declared as follows:

<attr name="visibility">
    <enum name="visible" value="0" />
    <enum name="invisible" value="1" />
    <enum name="gone" value="2" />
</attr>

所以,你可以通过它的价值直接引用枚举项目。事情是这样的:

So you can reference to enum item directly by its value. Something like this:

<!-- values.xml -->
<item name="viz" type="integer">1</item>

<!-- layouts.xml -->
<TextView
    android:visibility="@integer/viz"/>

我只是检查它在我的手机,它工作得很好。

I just checked it on my phone and it works well.

这篇关于参考属性枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆