android - 在 <API 21 中的颜色选择器中使用主题属性

标签 android android-layout

我有一个使用主题属性的状态列表颜色资源,即 button_background_tint.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/colorAccent" android:state_activated="true"/>
    <item android:color="?android:attr/colorBackground"/>
</selector>

并使用 app:backgroundTint="@color/button_background_tint" 将其应用于我的 FloatingActionButton 背景色调

该按钮在 API 21 或更高版本中看起来不错,但在 API 21 之下,无论其状态如何,背景都是红色的(即使 ?android:attr/colorBackgroundTheme.AppCompat.Light 的默认值)。

有没有办法在 API 21 之前的颜色选择器中引用主题属性? (如果重要,请使用支持库 25.3.0)

最佳答案

?android:attr/colorBackground在 API 21 之前没有定义。
使用 AndroidX 和 Material 组件时,您似乎可以使用 ?attr/colorSurface因为它是相同的颜色。我猜您可以使用它来避免为 v21 及更高版本定义特定资源。
Material 成分的背景值和表面值均定义为 there .

关于android - 在 <API 21 中的颜色选择器中使用主题属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42823986/

相关文章:

安卓工作室 : findViewId not working with custom views

android - 选择项目后,微调器在布局中向下移动

Android LinearLayout 宽度

android - 如何处理 :java. util.concurrent.TimeoutException : android. os.BinderProxy.finalize() 在 10 秒错误后超时?

android - GestureDetector.SimpleOnGestureListener。如何检测 ACTION_UP 事件?

Android google places API 超出每日配额

android - 在 ListView 中获取绝对 View 位置

Android如何通过用线条指向文字和图片来匹配文字和图片

android - 使用 RelativeLayout 时工具栏出现在布局顶部

Android Master Detail模板和ActionBar