更改Android 5.0滚动条颜色

时间:2014-11-18 15:55:30

标签: android android-5.0-lollipop material-design

有谁知道如何将Theme.Material中滚动条的颜色更改为colorControlNormal以外的其他颜色?如果你去看看themes_material你可以看到有某些Scrollbar属性,但没有直接的方法来覆盖滚动条颜色,只有整个drawable。为了我的目的,我想保持滚动条可绘制与系统使用的滚动条相同(相同的九个补丁),我只是想要使用与colorControlNormal颜色不同的颜色。

1 个答案:

答案 0 :(得分:2)

我发现您可以将自己主题中的整个scrollbarThumbVertical或scrollbarThumbHorizo​​ntal属性覆盖为不同的drawable;在你的AppTheme它看起来像:

<style name="AppTheme" parent="@android:style/Theme.Material">
    <item name="android:scrollbarThumbVertical">@drawable/scrollbar_handle_material</item>
</style>

然后在你的drawable-v21文件夹中你将从系统中找到相同的xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -->

<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
   android:src="@drawable/scrollbar_handle_mtrl_alpha"
   android:tint="#A4A4A4" />

您可以将色调颜色设置为您希望滚动条位于5.0+设备上的颜色。由于Google不会公开访问scrollbar_handle_mtrl_alpha资产,因此您需要从PATH_TO_ANDROID_SDK / platforms / android-21 / data / res / drawable-xxhdpi / scrollbar_handle_material_alpha以及您想要提供的所有其他密度中提取它。我将把drawable-xxhdpi资产放在这里作为参考:

enter image description here

如果你们找到任何其他方法可以做到这一点,请告诉我,而不必覆盖这么多东西。