是否支持API 26的文本颜色行为库? [英] Support library for text color behaviour of API 26?

查看:117
本文介绍了是否支持API 26的文本颜色行为库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

API 26引入了基于?colorForeground?textColorPrimary的高级颜色计算.它使用状态primaryContentAlphadisabledAlpha.

API 26 introduces an advanced color calculation for ?textColorPrimary based on ?colorForeground. It makes use of states, primaryContentAlpha and disabledAlpha.

sdk/platforms/android-26/data/res/color/text_color_primary.xml:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
        android:alpha="?attr/disabledAlpha"
        android:color="?attr/colorForeground"/>
    <item android:alpha="?attr/primaryContentAlpha"
        android:color="?attr/colorForeground"/>
</selector>

在API 23上,由于我没有弄清楚,它又变成了白色文本.

On API 23 it falls back to white text by means I failed to figure out.

我是否可以使用一个支持库来获取旧设备的API 26的颜色计算?

Is there a support library I could apply to get the color calculation of API 26 for older devices?

推荐答案

@ eugen-pechanec暗示缺少属性primaryContentAlphascondaryContentAlpha,恕我直言,低于API26.我们应该称其为bug还是缺失后端口?不知道.

@eugen-pechanec is hinting that the attributes primaryContentAlpha and scondaryContentAlpha are missing, IMHO below API 26. Should we call this a bug or a missing back port? Don't know.

结果是您不能使用设置?attr/colorForeground作为默认设置来自动创建所有前景色.基本上,您有两种选择,要么不使用它来进行手动后退端口.

The consequence is that you can't use the setting ?attr/colorForeground as a default to automatically create all foreground colours out of the box. You basically have two options, either not to use it to to do a manual back port.

您可以直接设置属性android:textColorPrimaryandroid:textColorSecondary,而不是从?attr/colorForeground生成颜色.在大多数情况下,这将是最佳选择.

Instead of generating the colours from ?attr/colorForeground you set the attributes android:textColorPrimary and android:textColorSecondary directly. This will be the best choice in most cases.

如果计划使用许多不同的主题,则希望启用该功能以将所有文本颜色设置为默认值.然后,您必须在根主题中实现API 26的行为.

If you plan to use a lot of different themes, you want to enable the feature to set defaults for all text colours in a central place. Then you have to implement the behaviour of API 26 in your root theme.

root theme:

    <!-- workaround to port back API 26+ behaviour -->

    <!-- below 26 these two attributes are missing in the android namespace -->
    <item name="primaryContentAlpha">1.0</item> 
    <item name="secondaryContentAlpha">.85</item>
    <!-- works below 26 -->
    <item name="android:disabledAlpha">.4</item>
    <!-- use my own files to connect my custom attributes -->
    <item name="android:textColorPrimary">@color/text_color_primary</item>
    <item name="android:textColorSecondary">@color/text_color_secondary</item> 

app/src/main/res/color/text_color_primary.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:alpha="?android:disabledAlpha" android:color="?android:attr/colorForeground" />
    <item android:alpha="?attr/primaryContentAlpha" android:color="?android:attr/colorForeground" />
</selector>

app/src/main/res/color/text_color_secondary.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:alpha="?android:disabledAlpha" android:color="?android:colorForeground"/>
    <item android:alpha="?secondaryContentAlpha" android:color="?android:colorForeground"/>
</selector>

这篇关于是否支持API 26的文本颜色行为库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆