android - 如何避免 CustomView 中资源 'attr/*' 的重复值

标签 android android-custom-view android-styles android-attributes

如果我导入:

自定义 View A (从 Maven 导入)

<declare-styleable name="CustomViewA">
        <attr name="min" format="float"/>
        <attr name="max" format="float"/>
</declare-styleable>

自定义 View B (从 Maven 导入)
<declare-styleable name="CustomViewB">
        <attr name="min" format="float"/>
        <attr name="max" format="float"/>
</declare-styleable>

这将失败说 分钟 最大 是重复的。我以为 Android 会通过 declare-styleable name 来区分,但猜不到。话虽如此,命名自定义 View 的最佳方法是什么 attr以避免将来出现任何可能的重复值冲突?

到目前为止,我得到的唯一解决方案是:
<attr name="minForMyCustomViewHopingNoOneUsesThisName" format="float"/>

这太糟糕了。

最佳答案

你可以这样做

<attr name="min" format="float" />
<attr name="max" format="float" />

<declare-styleable name="CustomViewA">
    <attr name="min" />
    <attr name="max" />
</declare-styleable>

<declare-styleable name="CustomViewB">
   <attr name="min" />
   <attr name="max" />
</declare-styleable>

关于android - 如何避免 CustomView 中资源 'attr/*' 的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60337829/

相关文章:

android - 像 Google Play 一样搜索编辑文本

android - 有没有办法设置 PreferenceFragmentCompat 的样式

android - 导航栏颜色不变

android - 无法启动 Activity 二进制 XML 文件错误膨胀类,在 Dex 路径列表中找不到类 - Android

android - 父样式取决于构建变体

android - 打开模态弹出窗口

android - 如何从范围内的所有网络提供商处获取所有手机信号塔的数据?

android - 启用/禁用来自android代码的使用数据包

Android Intel x86 Emulator KitKat (19) 无法启动

android - 通过 setter 更新 customView 中的颜色