【问题标题】:Android XML Layout Custom Attribute Values DropdownAndroid XML 布局自定义属性值下拉菜单
【发布时间】:2015-09-23 03:45:35
【问题描述】:

我正在寻找一种在 Android XML 的自定义属性中下拉可用值的方法,如下所示:

我做了什么:

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="font">
        <attr name="fontType" format="string" />
        <attr name="textSizeType" format="string" />
    </declare-styleable>
</resources>

这样使用:

<com.MyTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    font:fontType="regular"
    font:textSizeType="caption" />

稍后自定义属性的值将在 MyTextView 类中进行解释。

有什么方法可以下拉自定义属性的可用值?

【问题讨论】:

    标签: android android-xml


    【解决方案1】:

    尝试将您的属性定义为枚举:

    <attr name="my_attr">
      <enum name="value1" value="1" />
      <enum name="value2" value="2" />
    </attr>
    

    从您的屏幕截图看来,您仍在使用 Eclipse。我不知道 Eclipse ADT 插件会从你的枚举值中生成一个下拉列表,但你可以试试看。

    【讨论】:

    • 是的,我仍在使用 Eclipse。但是,当我完成输入 font: 并按 ctrl+space 显示下拉菜单时,字体标签本身不会显示自动完成下拉菜单。也许我应该尝试迁移到 Android Studio。
    • 在Android Studio中也可能不行,我没试过。我只知道 IDE 可以创建下拉列表的唯一方法是它是否知道该属性的所有合法值。
    猜你喜欢
    • 2022-07-06
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 2012-10-12
    相关资源
    最近更新 更多