如何在Lollipop 5.1上处理快速卷轴? [英] How to handle the fast-scroller on Lollipop 5.1?
问题描述
我已经制作了一个小库,其中显示了如何模仿Android Lollipop的股票联系人应用程序的样式, 此处 .
I've made a tiny library, which shows how to mimic the style of the stock contacts app of Android Lollipop, here.
似乎在Android 5.1上,快速滚动器看起来与前一个滚动器有很大不同,并且距离右侧太近,因此很难使用它.
It seems that on Android 5.1 , the fast scroller looks very different than the previous one, and it's too close to the right, so it's hard to use it.
Android 4.4的屏幕截图:
screenshot of Android 4.4 :
以下是Android 5的屏幕截图:
Here's a screenshot on Android 5 :
以及在Android 5.1上:
and on Android 5.1 :
我尝试遍历Android 5.1的所有新功能"部分以及一些相关的类文档,但是除了"
I've tried to go over all of the "what's new" section of Android 5.1, and also in some related classes docs, but I didn't find anything special, except for "setFastScrollStyle" . However, I couldn't find any explanation of how to use it (plus it's from API 21 , so that might not be the reason).
如何使快速滚动条位于左侧一点,以便于触摸它?
How can I make the fast scroller to be located a bit to the left, so that it will be easier to touch it?
如何使用setFastScrollStyle?有任何教程吗?
How do you use setFastScrollStyle? Is there any tutorial for this?
推荐答案
我遇到了同样的问题,经过几个小时的研究,我想出了一个解决方案.此AOSP提交对我的帮助最大,它显示了对Android 5.1(SDK 22)中的滚动条所做的更改:更新滚动条以符合材料规格
I had the same problem and after a few hours of research I came up with a solution. This AOSP commit helped me the most, it shows the changes made to the scrollbar in Android 5.1 (SDK 22): Update scrollbars to match Material spec
有两种可能性:
这将保留与API 21(Android 5.1)相同的新矩形,但在左右添加一些填充.
This will keep the same new rectangle from API 21 (Android 5.1), but add some padding left and right.
1:将fastscroll_thumb_material.xml复制到您的drawables文件夹中
这应该是内容(为了节省空间而删除了AOSP注释):
This should be the content (removed AOSP comments to save space):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:tint="?attr/colorControlActivated"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<size android:width="8dp" android:height="48dp" />
</shape>
</item>
<item>
<shape android:tint="?attr/colorControlNormal"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<size android:width="8dp" android:height="48dp" />
</shape>
</item>
</selector>
2:编辑主题
将以下项目添加到您的主题.我认为您可以将其放在一种样式中并与setFastScrollStyle
一起使用,但是我发现这更容易.
Add the following item to your theme. I think you could put this in a style and use it with setFastScrollStyle
, but I find this easier.
<item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb_material</item>
3:编辑fastscroll_thumb_material.xml
您可以根据自己的喜好对其进行编辑,但是我执行了以下操作,在滚动条的左侧和右侧添加了8dp.我在每个形状周围添加了一个图层列表和项目,并在新项目中添加了android:right="8dp"
和android:left="8dp"
.我尝试将其添加到原始项目中,但这没有用,也没有在形状中添加填充.
You can edit it to your liking, but I did the following, which adds 8dp to the left and right of the scrollbar. I added a layer-list and item around each shape and added android:right="8dp"
and android:left="8dp"
to the new item. I tried adding that to the original items, but that didn't work, neither did adding padding to the shape.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:right="8dp" android:left="8dp">
<shape android:tint="?attr/colorControlActivated"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<size android:width="8dp" android:height="48dp" />
</shape>
</item>
</layer-list>
</item>
<item>
<layer-list>
<item android:right="8dp" android:left="8dp">
<shape android:tint="?attr/colorControlNormal"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<size android:width="8dp" android:height="48dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
B:将滚动条更改为旧样式
这将使用API 21(Android 5.0风格)
B: Change the scrollbar to the old style
This will use the API 21, Android 5.0 style
1:将旧的可绘制对象添加到您的项目中
您可以从上面的提交中下载它们(fastscroll_thumb_mtrl_alpha.png和fastscroll_track_mtrl_alpha.9.png),但我也将它们捆绑在一起,也可以将它们下载为
You can download them from the commit above (fastscroll_thumb_mtrl_alpha.png and fastscroll_track_mtrl_alpha.9.png), but I also bundled them, you can also download them from my Google Drive.
2:将fastscroll_thumb_material.xml添加到可绘制对象中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/fastscroll_thumb_mtrl_alpha"
android:tint="?attr/colorControlActivated" />
</item>
<item>
<bitmap android:src="@drawable/fastscroll_thumb_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
</item>
</selector>
3:将fastscroll_track_material.xml添加到可绘制对象中
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/fastscroll_track_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
4:编辑您的主题
将以下项目添加到您的主题.我认为您可以将其放在一种样式中并与setFastScrollStyle
一起使用,但是我发现这更容易.
Add the following item to your theme. I think you could put this in a style and use it with setFastScrollStyle
, but I find this easier.
<item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb_material</item>
<item name="android:fastScrollTrackDrawable">@drawable/fastscroll_track_material</item>
我希望这有助于实现您的目标:)
I hope this helps to achieve your goal :)
这篇关于如何在Lollipop 5.1上处理快速卷轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!