【问题标题】:Why can't I use this attribute in my android theme?为什么我不能在我的 android 主题中使用这个属性?
【发布时间】:2012-10-29 10:29:26
【问题描述】:

从一些上下文开始,我尝试设置 SearchView 小部件的背景颜色。一个真正的insightful so answer 已经发布了这个问题,我从中学到了很多。

不过,我的理解存在一个差距,我希望有人可以向我解释。当我创建一个主题时,例如:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="MyCustomTheme" parent="android:Theme">
        <item name="android:searchViewTextField">@android:color/white</item>
    </style>
</resources>

Eclipse 编译时出现错误,提示它不知道该属性:

error: Error: No resource found that matches the given name: attr 'android:searchViewTextField'.

但是,如果我重新声明属性:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <declare-styleable name="CustomSearchView">
        <attr name="android:searchViewTextField" format="reference" />
    </declare-styleable>
</resources>

Eclipse 以错误消息响应:

error: Attribute "android:searchViewTextField" has already been defined

Eclipse 似乎知道该属性,但是当我想使用它时很方便地忘记了该属性。 (我想知道后台是否有一些上下文切换)

无论如何,如果我删除了问题代码,那么我可以看到我的自定义主题从其父主题继承了 searchViewTextField。我只是不明白为什么我不能用我自己的来代替它。

(另一个答案提到了 android.R.stylable,但该文件在 api 16 中已过时)

提前致谢。

相关 Android 来源:

themes.xmlattrs.xml 和 search_view.xml(对不起,两个链接限制)。

【问题讨论】:

    标签: android attributes styles themes searchview


    【解决方案1】:

    您明确链接的 SO 答案中的答案表明您无法在自己的主题中指定 searchViewTextField ,因为它不是可样式化的资源。您需要根据其他答案在代码中修改它的值。

    【讨论】:

    • 在 attrs.xml 文件中,searchViewTextField 属性在 中声明为可样式化。也许这导致我不明白。
    • 尝试只使用“searchViewTextField”,不带“android:”前缀。您应该能够在继承自 android:Theme 的自定义主题中覆盖它。
    • 我试了一下,但 Eclipse 报告“错误:未找到与给定名称匹配的资源:attr 'searchViewTextField'。”抱歉,除了 之外,我只是不明白使它们可用的机制。也许该属性在 Eclipse 构建中被删除了。
    【解决方案2】:

    问题是,为什么我不能使用主题属性“android:searchViewTextField”?

    由于怀疑编译后的 Android.jar 文件与发布的 Android 源代码不一致,我撕开 jar 并开始反编译(这对我来说是第一次)。在四处寻找之后,似乎有人带着砍刀进入了 R$style.class 并砍掉了大量资源。据我所知,这一定是故意的,甚至可能是手动完成的。

    这种编译后的源代码修改在其他类似框架中并非没有先例,但它肯定会让任何试图调试框架的人感到困惑。

    具有讽刺意味的是,自动生成的 R 文件都有一个标题,

    /* AUTO-GENERATED FILE.  DO NOT MODIFY.
    

    当你把修改文件变成例行程序时有点毫无意义。

    【讨论】:

      猜你喜欢
      • 2018-05-24
      • 2018-05-27
      • 1970-01-01
      • 2014-09-22
      • 2018-05-22
      • 2011-07-20
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      相关资源
      最近更新 更多