【问题标题】:Widget not tinted on Lollipop棒棒糖上没有着色的小部件
【发布时间】:2015-03-12 10:19:12
【问题描述】:

我正在为我的应用程序使用 appcompat v21.0.3。我所做的一切都像这里写的那样:android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

但在 Lollipop 上(当然还有旧设备上),一些小部件没有用我的强调色着色。例如:

  • SwitchCompat 有色:

  • ListPreference 未着色

  • ProgressDialog 未着色

这是我的代码:

build.gradle

...
compile 'com.android.support:appcompat-v7:21.0.+'
...

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CET"
        android:hardwareAccelerated="true"
        tools:replace="label">

themes.xml

<resources>

    <style name="CET" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
    </style>

</resources>

colors.xml

<resources>

    <!-- App branding color -->
    <color name="primary">#a32b30</color>

    <!-- Darker variant for status bar and contextual app bars -->
    <color name="primary_dark">#000000</color>

    <!-- Theme UI constrols like checkboxes and text fields -->
    <color name="accent">#a32b30</color>

</resources>

有人有想法吗?

更新:截至 2015 年 6 月,仍然无法正常工作,但我最终使用了 https://github.com/afollestad/material-dialogs。非常适合对话框,包括 ListPreferences。

【问题讨论】:

    标签: android android-widget android-5.0-lollipop android-appcompat


    【解决方案1】:

    按照以下指南进行操作

    除非另有说明,否则以下所有着色均适用于使用 AppCompat v21 的 Lollipop 和 pre-Lollipop。要使用这些属性的支持版本,请删除 android 命名空间。例如,“android:colorControlNormal”变为“colorControlNormal”。这些属性将传播到运行 Lollipop 的设备的 android 命名空间中的相应属性。任何例外情况都将通过包含“android:”前缀来注明。

    All Clickable Views:
    -----------
    * ripple effect (Lollipop only) -- "colorControlHighlight"
    
    Status Bar:
    ------------
    * background (Lollipop only) - "colorPrimaryDark"
    
    Navigation Bar:
    ----------------
    * background (Lollipop only) - "android:navigationBarColor"
    
    EditText:
    ----------
    * underline (unfocused) -- "colorControlNormal"
    * underline overlay (focus) -- "colorAccent"
    * cursor -- "colorAccent"
    * text color -- "android:textColorPrimary"
    
    CheckBox:
    ----------
    * box unchecked -- "colorControlNormal"
    * box checked -- "colorAccent"
    
    RadioButton:
    -------------
    * unselected -- "colorControlNormal"
    * selected -- "colorAccent"
    * ripple effect (Lollipop only) -- "colorControlHighlight"
    
    SwitchCompat:
    -------------
    * thumb switch off -- "colorSwitchThumbNormal"
    * thumb switch on -- "colorAccent"
    * track overlay (when switched on) -- "colorAccent"
    
    Spinner:
    ---------
    * indicator (not pressed) -- "colorControlNormal"
    * indicator (pressed) -- "colorAccent"
    * selected entry text color (Lollipop only) -- "android:textColorPrimary"
    
    ActionBar:
    -----------
    * background -- "colorPrimary"
    * title color -- "android:textColorPrimary"
    * overflow icon -- "android:textColorPrimary"
    * up button -- "android:textColorPrimary"
    * action icons -- "android:textColorPrimary" †
    * overflow menu background -- "android:colorBackground"
    * overflow text color -- "android:textColorPrimary"
    
    Toolbar (Theme Overlay should be used):
    ----------------------------------------
    * background -- must be set manually in XML. Can do (android:background="?attr/colorPrimary")
    * overflow icon -- "android:textColorPrimary"
    * navigation icon -- "android:textColorPrimary" †
    * action icons -- "android:textColorPrimary" †
    * overflow menu background -- "android:colorBackground"
    * overflow text color -- "android:textColorPrimary"
    

    附:默认情况下,着色仅适用于列入白名单的股票操作图标(请参阅TintManager 源代码)。例如,后退箭头图标"abc_ic_ab_back_mtrl_am_alpha" 是有色的,但是复制该文件并重命名它会导致图标不被着色,而随机拍摄图像并将其重命名为"abc_ic_ab_back_mtrl_am_alpha" 将导致它被着色。通过在可绘制对象中创建&lt;bitmap&gt; xml 文件并应用"android:tint" 属性,可以在Lollipop 中的XML 中进行着色。这个图标可以在 Lollipop 和 pre-Lollipop 中使用,但它只会在 Lollipop 中着色。也可以使用ColorFilter 以编程方式对动作图标进行着色。

    Source

    【讨论】:

    • 感谢您提供详细的答案,但这无济于事:ProgressDialog 应该在 Lollipop 上着色,而无论我使用什么值,它都不是(我尝试添加每个值(colorControlNormal,.. .) 它没有改变:有些元素是绿色的。
    • 尝试在一个特定元素(按钮)上执行此操作以进行测试android:buttonTint="@color/colorAccent"android:button="@drawable/abc_btn_check_material"
    • 我会尝试检查,但问题不在于我自己使用的小部件,而在于我无法访问的小部件:标准 ProgressDialog 和 PreferenceFragment 中使用的 ListPreference。所以我不能在这些上添加这个元素...
    【解决方案2】:

    它只回答了您的部分问题,但 appcompat 库中的进度条样式还没有选择棒棒糖上的强调色,即使在 ProgressDialog 之外也是如此。

    您需要自己设置它,例如使用这种样式:

    <style name="TintedProgressBar" parent="Widget.AppCompat.ProgressBar">
        <item name="android:indeterminate">true</item>
        <item name="android:indeterminateTint">?attr/colorAccent</item>
        <item name="android:indeterminateTintMode">src_in</item>
    </style>
    

    然后您可以申请ProgressBarContentLoadingProgressBar。 (请注意,进度条膨胀的父元素显然必须应用定义 colorAccent 属性的主题;否则使用直接颜色资源)

    如果你想在对话框中集成进度条,因为我不认为ProgressDialog支持自定义进度条超出类型和可绘制,你可能需要设置一个AlertDialog应用上述主题的进度条,或者只是使用自定义对话框我猜您可以参考this answer将上述与进度条相关的属性应用于对话框。

    【讨论】:

      【解决方案3】:

      要更改进度对话框可绘制的颜色,我使用了以下解决方法:我创建了 AppCompat 警报对话框基本样式

          <style name="CustomAppCompatAlertDialogStyle"    parent="Theme.AppCompat.Light.Dialog.Alert">
                  <item name="colorAccent">@color/defaultColor</item>
                  <item name="android:indeterminateTint">?attr/colorAccent</item>
                  <item name="android:indeterminateTintMode">src_in</item>
          </style>
      

      然后将这些主题应用到进度对话框:

      mProgressDialog = new ProgressDialog(this, R.style.CustomAppCompatAlertDialogStyle);
      

      【讨论】:

        【解决方案4】:

        您通常应该使用自己的进度条而不是对话框,然后您可以制作一个适用于 L 和 pre-L 的着色版本,如下所示:

        public class TintedProgressBar extends ProgressBar {
            public TintedProgressBar(Context context) {
                super(context);
                init();
            }
        
            public TintedProgressBar(Context context, AttributeSet attrs) {
                super(context, attrs);
                init();
            }
        
            public TintedProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
                super(context, attrs, defStyleAttr);
                init();
            }
        
            public void init() {
                getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.primary), PorterDuff.Mode.SRC_IN);
                getProgressDrawable().setColorFilter(getResources().getColor(R.color.primary), PorterDuff.Mode.SRC_IN);
            }
        }
        

        如果你真的想改变对话框的颜色,你可以在dialog.findViewById(android.R.id.progess)找到嵌套的进度条

        【讨论】:

          【解决方案5】:

          据我所知,对话框中的任何内容都不会在 Lollipop 中自动着色(目前)。 Checkout material-dialogs,一个自动为您执行此操作的库(但它仅支持 API 14+)。

          查看类似问题的答案:Android v21 Theme.Appcompat color accent is ignored, no padding on dialogs

          【讨论】:

          • 所以要在 Lollipop 上设置有色对话框,我不能使用 AppCompat?
          • 如果您需要支持较低级别的 API,请使用 AppCompat,如果只有 Lollipop 则不需要。我指的库是独立于AppCompat的,我觉得可以一起使用。
          【解决方案6】:

          试试属性colorControlActivated:

          主题.xml

          <style name="CET" parent="Theme.AppCompat.Light.NoActionBar">
              <item name="windowActionBar">false</item>
              <item name="colorPrimary">@color/primary</item>
              <item name="colorPrimaryDark">@color/primary_dark</item>
              <item name="colorAccent">@color/accent</item>
              <item name="colorControlActivated">#a32b30</item>
          </style>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-09-10
            • 2015-08-12
            • 1970-01-01
            • 1970-01-01
            • 2023-03-21
            • 1970-01-01
            • 2015-09-01
            • 2016-10-23
            相关资源
            最近更新 更多