如何自定义快速滚动预览字母

4
我需要定制快速滚动字母预览(截图中的大M)并使用自定义背景资产,但我找不到任何相关文档。你能帮我吗?

请查看http://stackoverflow.com/q/24176990/1374015 - Pedro
看一下我的答案。 - Santacrab
2个回答

6
感谢 @MAB 和查看Lollipop源代码的帮助,我成功获得了我需要的东西。
我的 c_fastscroller_preview.xml 文件:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<corners
    android:topLeftRadius="44dp"
    android:topRightRadius="44dp"
    android:bottomLeftRadius="44dp" />
<padding
    android:paddingLeft="22dp"
    android:paddingRight="22dp" />

<solid android:color="@color/c_red_e60000" /></shape>

c_fastscroller_thumb.xml 的位置:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@drawable/c_fastscroll_thumb" />
<item
    android:drawable="@drawable/c_fastscroll_thumb" /></selector>

在我的应用程序中,styles.xml 文件中:

        <!-- This belongs in a FastScroll style -->
    <item name="android:fastScrollThumbDrawable">@drawable/c_fastscroller_thumb</item>
    <item name="android:fastScrollPreviewBackgroundRight">@drawable/c_fastscroller_preview </item>
    <item name="android:fastScrollOverlayPosition">aboveThumb</item>
    <item name="android:fastScrollTextColor">@color/c_white</item>

它在Lollipop上完美运行,但在之前的Android版本中,fastScrollPreview根本没有显示。我该怎么解决? - Santacrab
奇怪!似乎Holo不能处理aboveThumb,只能处理floating和atThumb(这是holo源代码中的默认设置)。但是使用其中一个,材料形状不适合。因此,您可以尝试使用特定的Holo样式,具有不同的覆盖位置和预览。 - Pedro

2
您可以通过在res/values/styles.xml下创建自定义样式来实现这一点。
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb</item>
    <item name="android:fastScrollOverlayPosition">atThumb</item>
    <item name="android:fastScrollTextColor">@color/your_color</item>
    <item name="android:fastScrollTrackDrawable">@drawable/fastscroll_thumb_pressed</item>
</style>

其中fastScroll_thumb是一个选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/fastscroll_thumb_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/fastscroll_thumb_default"/>
</selector>

fastfastscroll_thumb_pressed/fastscroll_thumb_default是可自定义的可绘制对象,您可以根据自己的喜好进行自定义。

提示:不要忘记在清单文件中将样式设置为您的活动。

如果我错了,请纠正我,但我认为已经有很多讨论相同问题的问题了。

祝你好运。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接