【问题标题】:Meaning of 'android:' prefix within attribute value (e.g. "android:imageButtonStyle")属性值中“android:”前缀的含义(例如“android:imageButtonStyle”)
【发布时间】:2017-09-12 13:41:18
【问题描述】:

我想在一个主题中设置我所有的 ImageButtons 样式。经过一段时间的搜索,我找到了解决问题的方法。但我不知道为什么会这样。

我的主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher_foreground" />
</LinearLayout>

这是我原来的不起作用的主题。它设置了我的 TextView 的样式,但忽略了 ImageButton。结果如下面的截图所示。

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:imageButtonStyle">@style/redBackground</item>
        <item name="android:textViewStyle">@style/redBackground</item>
    </style>

    <style name="redBackground">
        <item name="android:background">#FF0000</item>
    </style>
</resources>

这是有效的主题

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="imageButtonStyle">@style/redBackground</item>
        <item name="android:textViewStyle">@style/redBackground</item>
    </style>

    <style name="redBackground">
        <item name="android:background">#FF0000</item>
    </style>
</resources>

唯一的区别是“imageButtonStyle”属性前面缺少“android:”前缀。

所以我的问题是:

  • imageButtonStyle 和 android:imageButtonStyle 有什么区别?
  • 为什么 android:textViewStyle 有效但 android:imageButtonStyle 无效?它们都被定义为 'attrs.xml' 平台。
  • 为什么没有textViewStyle(没有android前缀)?删除前缀会产生错误。
  • 没有前缀的属性定义在哪里?显然不在平台“attrs.xml”中。
  • 我在哪里可以找到适合整个风格的文档?当然,我已经阅读了相应的 Google 文档 (https://developer.android.com/guide/topics/ui/look-and-feel/themes.html)。但我仍然有类似这样的基本问题。

有趣的是,'android:imageButtonStyle' 版本似乎在几年前就可以使用:How to apply an style to all ImageButtons in Android?。不过,我自己还没有测试过。

这是建议删除 android 前缀的帖子。包括询问它为什么起作用的未回答的 cmets:buttonStyle not working for 22.1.1

【问题讨论】:

    标签: android


    【解决方案1】:

    您使用的android标签用于来自Android SDK的属性。

    如果您使用支持库,则使用 app 标签。app 只是自定义 View 的任何自定义参数的命名空间。

    这可以是任何东西,但如果您看到根元素,则可能有一行 xmlns:app="http://schemas.android.com/apk/res-auto" 分配了命名空间

    如果您使用自定义视图(您自己的或形成一个库),您也可能会看到其他命名空间。

    【讨论】:

    • 贴出的代码是完整的。我没有遗漏任何命名空间声明。但这引出了下一个问题:尽管我没有使用 ImageButton 支持库,但为什么要应用 AppCompat 属性 (srcCompat)?我从来没有真正意识到这一点..
    • 好的,我对支持库布局充气器做了一些研究,总体上回答了我在评论中的问题,但不幸的是不是我原来的问题。
    【解决方案2】:

    如果其他人偶然发现这个问题:我在这个 Droidcon 演讲中找到了答案:https://youtu.be/Jr8hJdVGHAk?t=21m12s

    从 21:12 开始,主题在一分钟内处理完毕。

    据我了解,不指定命名空间会导致搜索全局命名空间,这似乎包括支持库属性。事实上,SDK's R.attrsupport library's R.attr 都定义了imageButtonStyle 属性(描述略有不同)。但是,支持库没有定义textViewStyle 属性。这就解释了为什么不能省略 android: 前缀。

    回答我关于文档的最后一个问题:尽管有 Google 指南和 R.attr 类的文档,但上面提到的视频和这个 Google I/O 演讲提供了很多信息:https://www.youtube.com/watch?v=TIHXGwRTMWI

    所以唯一悬而未决的问题是为什么 SDK 的 imageButtonStyle 不起作用。

    【讨论】:

    • "为什么 SDK 的 imageButtonStyle 不起作用" - 您显然在 AppCompatActivity 中,如果找到它们,它会自动创建某些 SDK Views 的 AppCompat 版本在布局中。 ImageButton 是“升级”的其中之一,AppCompatImageButton 将在应用程序的命名空间中使用 imageButtonStyle 属性。
    猜你喜欢
    • 2015-03-12
    • 1970-01-01
    • 2014-05-25
    • 2017-09-07
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    相关资源
    最近更新 更多