如何避免 CustomView 中资源“attr/*”的重复值

Gui*_*lhE 5 android android-custom-view android-styles android-attributes

如果我导入:

CustomViewA(从Maven导入)

<declare-styleable name="CustomViewA">
        <attr name="min" format="float"/>
        <attr name="max" format="float"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

CustomViewB(从Maven导入)

<declare-styleable name="CustomViewB">
        <attr name="min" format="float"/>
        <attr name="max" format="float"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

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

到目前为止我得到的唯一解决方案是:

<attr name="minForMyCustomViewHopingNoOneUsesThisName" format="float"/>
Run Code Online (Sandbox Code Playgroud)

这很糟糕。

Pri*_*agb 5

你可以这样做

<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>
Run Code Online (Sandbox Code Playgroud)


Rom*_*man 0

使其独一无二是最终的答案。我准备使用

<attr name="custom_view_a_min" format="float"/>
<attr name="custom_view_b_min" format="float"/>
Run Code Online (Sandbox Code Playgroud)