【问题标题】:appcompat v22.1 widget tinting doesn't show pre 5.0appcompat v22.1 小部件着色不显示 5.0 之前的版本
【发布时间】:2015-05-21 19:57:50
【问题描述】:

我正在使用 AppCompat lib v22.1 为多个 android 版本提供一些材料设计。如果我使用新的小部件着色功能,即使使用 AppCompat,它也不会出现在 Kitkat 中。

在布局中:

<Button
            android:id="@+id/mybtn"
            android:text="Test"
            android:theme="@style/MyTheme.BlueButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

在主题 xml 中:

<style name="MyTheme.BlueButton">
    <item name="colorButtonNormal">@color/blue</item>
</style>

这是我做错了什么还是 AppCompat 中的错误?

【问题讨论】:

    标签: android android-appcompat android-theme


    【解决方案1】:

    通过使用以下设置,我能够在 Kitkat 和 Lollipop 上达到预期的效果:

    build.gradle(部分)

    dependencies {
        compile 'com.android.support:appcompat-v7:22.1.1'
    }
    

    值/样式.xml

    <resources>
    
        <style name="Theme.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        </style>
    
        <style name="ThemeOverlay.AppTheme.Blue" parent="ThemeOverlay.AppCompat.Dark">
            <!-- ThemeOverlay themes define only some attributes, those which make
                backgrounds dark and highlights and texts light for ThemeOverlay.AppCompat.Dark
                or the other way around for .Light. The rest is taken from the activity theme.
                You only customize what you need - here the button color. -->
            <item name="colorButtonNormal">@color/blue_500</item>
        </style>
    
        <style name="ThemeOverlay.AppTheme.Yellow" parent="ThemeOverlay.AppCompat.Light">
            <!-- This worked correctly even with parent="Theme.AppTheme". -->
            <item name="colorButtonNormal">@color/yellow_500</item>
        </style>
    
    </resources>
    

    layout/activity_main.xml(部分)

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"/>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:theme="@style/ThemeOverlay.AppTheme.Blue"/>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:theme="@style/ThemeOverlay.AppTheme.Yellow"/>
    

    编辑:(哦,是的,我忘了)MainActivity.java

    public class MainActivity extends AppCompatActivity { ... }
    

    结果

    【讨论】:

    • 当我将此代码放入一个新项目时,它可以工作。它甚至可以在不设置样式父级的情况下工作。不过,它在我原来的项目中仍然不起作用。不过,我已经找到了原因。
    • 你能分享一下它最初不与其他人合作的原因吗?
    • 当然,看我添加的答案。
    【解决方案2】:

    我发现为什么原来的代码在我认为应该可以工作的时候却不能工作。我使用的是支持 v4 库中的 FragmentActivity,它似乎没有内置主题的新 v7 AppCompat 功能。将我的活动更改为 AppCompatActivity 使主题工作与我所拥有的没有任何变化。

    这里有更多技术解释:https://stackoverflow.com/a/29978777/3517023

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多