【问题标题】:android listview fast scroll customization issueandroid listview快速滚动自定义问题
【发布时间】:2012-02-29 14:17:44
【问题描述】:

这是我的列表视图

<ListView android:layout_width="match_parent"
            android:layout_height="match_parent" android:id="@+id/ListView"
            android:fastScrollEnabled="true" android:divider="@null"
            style="@drawable/listviewfastscrollstyle"
            ></ListView>

这是 listviewfastscrollstyle 样式文件

<style> 
<item name="android:fastScrollTrackDrawable">@drawable/listselector</item> 
<item name="android:fastScrollThumbDrawable">@drawable/listselector</item> 
</style>

这是列表选择器文件

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

但列表视图快速滚动条仍然没有得到自定义。

【问题讨论】:

  • 不要对您的 ListView 做任何事情。创建样式,将其应用到 Manifest 中的 Application 标签。

标签: android listview fastscroll


【解决方案1】:

您创建的样式不正确。你需要给它一个名字。我不确定你上一篇关于这个的帖子发生了什么。执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="listviewfastscrollstyle" parent="android:Theme">
    <item name="android:fastScrollTrackDrawable">@drawable/listselector</item>
    <item name="android:fastScrollThumbDrawable">@drawable/listselector</item>
</style>

</resources>

在您的清单中设置如下样式:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/CustomTheme">

根据要求,这是我正在测试的 ListView:

<ExpandableListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:fastScrollAlwaysVisible="true"
    android:fastScrollEnabled="true"
    android:indicatorLeft="8dip"
    android:indicatorRight="52dip"
    android:paddingRight="32dip" />

【讨论】:

  • 样式的文件名也应该是CustomTheme.xml吗?
  • xml文件的名称是任意的,但必须使用.xml扩展名并保存在res/values/文件夹中。
  • 我已将样式文件直接放在名为 listviewfastscrollstyle.xml 的 values 文件夹中。但我收到“D:\30_JAN_2012\res\values\listviewfastscrollstyle.xml:1: error: Invalid start tag Style”的编译错误。这是
  • 只需复制并粘贴我的编辑。一切正常。请把答案标记为正确。
  • 我当然会接受:)。但它仍然不起作用..这是样式代码,我已将其直接放在值文件夹中
【解决方案2】:

要更改fastScrollThumbDrawablefastScrollTrackDrawable 或快速滚动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:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/ListView"
    android:fastScrollEnabled="true" 
    android:divider="@null"/>

如果您需要它,您的 ThumbDrawable 可能看起来像这样:

fast_scrollbar_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="10dp" android:bottom="10dp">
        <shape android:shape="rectangle">
            <size
                android:height="45dp"
                android:width="5dp" />
            <solid android:color="#DA414A" />
        </shape>
    </item>
</layer-list>

AFAIK 没有理由在 HoneyComb (API 11) 之前设置 FastScroll 栏的样式

【讨论】:

  • 非常感谢!您知道我是否可以更改使用此方法滚动时出现的标签的文本大小?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多