安卓 ListView 拇指滚动条

5

我想要看到以下的拇指滚动条:

http://www.androidpatterns.com/uap_pattern/scroll-thumb

我所看到的是:

enter image description here

我的XML布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="xxaassss"
        ads:loadAdOnCreate="true" >
    </com.google.ads.AdView>

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

我的Java代码 在Activity类的onCreate方法中(我扩展自Activity类而不是ListActivity):

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
ListView list=(ListView)findViewById(R.id.list);
ArrayList<HashMap<String, String>> categoriesList = new ArrayList<HashMap<String, String>>();

        for(int i=0;i<119;i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(KEY_ID, String.valueOf(i));
            //map.put(KEY_TITLE, "hello");
            map.put(KEY_TITLE, Prayer.tip.getNthLine(i, true).replace("~", ""));
            map.put(KEY_ARTIST, "");
            map.put(KEY_DURATION, "");
            map.put(KEY_THUMB_URL, String.valueOf(getResources().getIdentifier("img" + i, "drawable", getPackageName())));
            categoriesList.add(map);

        }
        LazyAdapter adapter=new LazyAdapter(this, categoriesList);
        list.setAdapter(adapter);

        list.setFastScrollEnabled(true);

我没有发布LazyAdapter的代码,因为我认为它与快速滑动滚动条的样式无关。如果需要,我也可以发布该代码。


https://dev59.com/xHA75IYBdhLWcg3w186G - Brijesh Thakur
1
你看到滚动条拇指是因为你在Android 4.0或更高版本上运行应用程序。链接中的拇指来自Android 2.3。你可以从@Brijesh上面发布的链接获取该图像并使用它。 - Milan
2个回答

7
将您的 XML 代码更改为以下内容:
<com.google.ads.AdView
    android:id="@+id/adView"`enter code here`
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="xxaassss"
    ads:loadAdOnCreate="true" >
</com.google.ads.AdView>

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:divider="#FFF"
    android:fadeScrollbars="false"
    style="@style/CustomTheme"
    android:scrollX="0px"
    android:scrollY="0px"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbarSize="20dp"
    android:scrollbarStyle="outsideOverlay"
    android:scrollbars="vertical" />

并按照您的风格

<style name="CustomTheme" parent="android:Theme">
<item name="android:scrollbarTrackVertical">@drawable/scroll_track</item>
<item name="android:scrollbarThumbVertical">@drawable/scroll_thumb</item>

就像在清单文件(manifest)中一样

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


1
我添加了这段代码,但是报告了styles.xml中的错误。有没有地方可以获取scroll_track和scroll_thumb图片? - Piyush-Ask Any Difference

1

只需更改android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb",并根据您的需要创建一个可绘制对象。

例如:

<ListView android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb" /> 

在Drawable中:
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <gradient android:angle="0" android:endColor="#6699FF" android:startColor="#3333FF" />
    <corners android:radius="1dp" /> 
    <size android:width="1dp" /> 
</shape>

我想这就是你想要的!

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