【问题标题】:Separate day/night selector for button text color按钮文本颜色的单独日/夜选择器
【发布时间】:2022-01-12 06:37:57
【问题描述】:

我为按钮文本颜色创建了一个自定义选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?android:attr/textColorPrimary" android:state_enabled="true" />
    <item android:alpha="0.38" android:color="?android:attr/textColorPrimary" />
</selector>

但是,我想在夜间主题中使用?android:attr/textColorPrimary,在白天主题中使用?android:attr/colorPrimary。我怎样才能做到这一点?

【问题讨论】:

  • 你应该使用样式(主题)而不是选择器,你可以创建textcolor样式并设置为xml按钮。

标签: android android-xml android-button android-theme android-styles


【解决方案1】:

在其中创建文件夹drawable-night,将夜间变量放入其中,将未修改的文件保留在公共drawable文件夹中

【讨论】:

    【解决方案2】:

    在日/夜模式设置中,每个属性都将具有互补的浅色和深色。您可以在 theme.xml 中为浅色模式和深色模式创建一个新项目并使用它。

    res 目录中创建一个values-night 文件夹,并在其中创建一个themes.xml 这将包含您的夜间主题。 values 文件夹中的 themes.xml 包含您的一天主题。

    例如说:这是你在values文件夹中的theme.xml

    <resources xmlns:tools="http://schemas.android.com/tools">
    
    <style name="Theme.MyApp" parent="Base.Theme.MyApp">
    
        <item name="buttonColor">@color/light_base_1</item>
    
    </resources>
    

    这是您在values-night 文件夹中的theme.xml

        <resources xmlns:tools="http://schemas.android.com/tools">
    
    <style name="Theme.MyApp" parent="Base.Theme.MyApp">
    
        <item name="buttonColor">@color/dark_base_1</item>
    
    </resources>
    

    attrs.xml 文件中创建一个属性

        <attr name="buttonColor" format="color" />
    

    然后你可以在选择器中使用这个新属性

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?android:attr/textColorPrimary" android:state_enabled="true" />
    <item android:alpha="0.38" android:color="?attr/buttonColor" />
    

    【讨论】:

      猜你喜欢
      • 2016-10-10
      • 2021-12-16
      • 1970-01-01
      • 2016-03-30
      • 2011-08-29
      • 1970-01-01
      • 2013-09-28
      • 2015-10-29
      • 2015-09-28
      相关资源
      最近更新 更多