2012-02-19 216 views
2

我正在做一个应用程序,我希望我的listview上的fastscroll选项卡是一个自定义的拇指选项卡。阅读文档在线我认为这将做到这一点:Android ListView fastScrollThumbDrawable不工作

<ListView 
android:id="@android:id/list" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:drawSelectorOnTop="false" 
android:cacheColorHint="@color/myWhite" 
android:scrollbars="vertical" 
android:scrollbarThumbVertical="@drawable/scrollthumb" 
android:scrollbarSize="12dip" 
android:fastScrollThumbDrawable="@drawable/scrollbar_thumb" 
android:fastScrollEnabled="true" 
/> 

列表视图是罚款,自定义滚动条的颜色正在和fastscrollbar选项卡显示,但它使用的是默认的拇指图像,而不是PNG文件scrollbar_thumb

拇指图像是否需要以某种格式或大小? 是否可以更改为自定义图形,如果不可以,至少可以更改拇指的颜色?

任何帮助将非常感激

回答

0

注意,属性仅适用于Android API级别11及更高版本。

http://developer.android.com/reference/android/R.attr.html

+0

矿仍然无助于ICS 。我必须将主题应用到我的应用程序才能获得自定义快速滚动条。 – sgarman 2012-05-15 22:48:22

+1

@sgarman,能否请你解释一下(作为答案吧?)你的意思是将你的应用程序的主题应用到自定义快速滚动大拇指上? – Rick 2012-09-12 15:19:35

2

在ListView XML定义,添加

android:fastScrollEnabled="true" 

或代码

listView.setFastScrollEnabled(true); 

创建在res /可绘制文件夹文件fastscroll_thumb.xml如下:

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

在AndroidManifest.xml中,设置自定义主题,为您的应用:

<application 
    android:theme="@style/ApplicationTheme" 
    ...> 

创建res文件夹一个文件夹值。创建RES /值的themes.xml文件内容如下:

<resources> 
    <style name="ApplicationTheme"> 
     <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb</item> 
    </style> 
</resources> 

最后确保fastscroll.png和fastscroll_pressed.png在绘制文件夹中存在

+0

我喜欢你的明确解释。我不明白,为什么我需要在AndroidManifest中设置android:主题,而不是LinearLayout元素中的另一个布局文件。 – 2015-02-19 23:27:12

+0

在编写它的android版本中,更改快速滚动缩略图的唯一方法是使用应用程序主题在整个应用程序中设置它。有可能在Android的更新版本中将其设置为LinearLayout,但我将不得不检查该内容并回复给您。 – JeffG 2015-05-07 19:43:51

0

我使用的是,但我不知道为什么它不工作,所以在网上搜索我发现here一个硬代码解决方案,我不知道它是否适用于旧的API,但在我的情况是解决了这个问题。注意我正在使用API​​ 18,如目标和带有API 17的设备来测试。

代码:

try { 
    Field f = AbsListView.class.getDeclaredField("mFastScroller"); 
    f.setAccessible(true); 
    Object o = f.get(<<your listView here>>); 
    f = f.getType().getDeclaredField("mThumbDrawable"); 
    f.setAccessible(true); 
    Drawable drawable = (Drawable) f.get(o); 
    drawable = getResources().getDrawable(R.drawable.<<your thumb drawable here can be a selector>>); 
    f.set(o, drawable); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
0

为了改变fastScrollThumbDrawable,将fastScrollTrackDrawable,或fastscroll SectionIndexer你必须使用一个上下文主题的文本颜色。其他答案建议通过AndroidManifest覆盖应用程序的主题来执行此操作。这确实有效,但如果你想要不同的滚动条外观每ListView你不能这样做。此外,您更改SectionIndexer上的文字颜色的方式不应在您的应用主题中完成,因为它可能具有其他不良效果。

为快速滚动设置ListView的最佳方式是创建一个使用ContextThemeWrapper的自定义ListView

下面是一个例子:

public class FastscrollThemedListView extends ListView { 
    public FastscrollThemedListView(Context context, AttributeSet attrs) { 
     super(new ContextThemeWrapper(context, R.style.FastScrollTheme), attrs); 
    } 
} 

这就是你所需要的。你的风格看起来就像这样:

<style name="FastScrollTheme"> 
    <item name="android:textColorPrimary">?android:textColorPrimaryInverse</item> 
    <item name="android:fastScrollThumbDrawable">@drawable/fast_scrollbar_thumb</item> 
    <item name="android:fastScrollTrackDrawable">@drawable/fast_scrollbar_track</item> 
</style> 

textColorPrimary是你怎么挂钩,你如何挂钩到SectionIndexer字体颜色,如果你使用它。

你的ListView应该是这样的:

<com.yourapp.view.FastscrollThemedListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:drawSelectorOnTop="false" 
    android:cacheColorHint="@color/myWhite" 
    android:scrollbars="vertical" 
    android:scrollbarSize="12dip" 
    android:fastScrollEnabled="true"/> 
0

我测试了以前的答案,并与该简化版瓦下为快速滚动的自定义拇指图标/最低要求上来:

Android清单: (应用标签)

android:theme="@style/ApplicationTheme"

RES /值/的themes.xml:

<resources> 
    <style name="ApplicationTheme"> 
    <item name="android:fastScrollThumbDrawable">@drawable/scrollbar</item> 
    </style> 
</resources> 

布局\ activity_main.xml中: (或任何你的观点是,要将此应用到&实现快速滚动) 添加此attibute:

<ListView android:fastScrollEnabled="true" />