开发者

Referencing enumerated style attributes from XML in Android

开发者 https://www.devze.com 2023-01-05 05:42 出处:网络
I have an app that requires scrollbars to be enabled in one landscape orientation but not in portrait orientation.I thought the easiest way to do this would be to make an attribute indicating whether

I have an app that requires scrollbars to be enabled in one landscape orientation but not in portrait orientation. I thought the easiest way to do this would be to make an attribute indicating whether the scrollbar is enabled or not, such as the following:

<ScrollView
        a:layout_width="fill_parent"
        a:layout_height="fill_parent"
        a:scrollbars="?my_activity_scrollbars"
        a:fadingEdge="none"
        >

I would then define separate values of the attribute for landscape and portrait modes. Seemed easy.

So I then defined this attribute in attrs.xml:

<resources>
  <declare-styleable name="Theme">
    <attr name="my_activity_scrollbars" format="enum" /> 
  </declare-styleable>
</resources>

And added it to my app's styles.xml:

<style name="MyApp" parent="@android:style/Theme.Light">
    <item name="add_credit_card_scrollbars">none</item> 
</style>

However, when I try to compile my app, I get the following error:

styles.xml:8: error: Error: String types not allowed (开发者_StackOverflow社区at 'my_activity_scrollbars' with value 'none').

It seems clear that it's treating "none" as a string rather than an enumerated value. Rather than using "none" I tried things like "?android:attr/scrollbars/none", "?android:attr/none", etc, but those didn't work.

How can I specify the "none" value as an enumerated value instead of a string?


I believe you have your attr definition set incorrectly:

This is an attr enum from the android source:

<attr name="ellipsize">
    <enum name="none" value="0" />
    <enum name="start" value="1" />
    <enum name="middle" value="2" />
    <enum name="end" value="3" />
    <enum name="marquee" value="4" />
</attr>

It appears you do not define format="enum" and instead specify the enum values within the attr tag itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号