http://blog.csdn.net/xyang81/article/details/17680333 html
在eoe上看到有個貼子經過反射,動態修改FastScroller對象的mThumbDrawable屬性來改變快速滑動塊的圖標,這也不爲於一種實現方式,但反射的效率較低。下面將介紹使用Style的方式來自定義圖標。java
從FastScroller類的init方法中能夠得知,mThumbDrawable是經過獲取當前Activity主題的android.R.attr.fastScrollThumbDrawable屬性賦值,既然是這樣的話,咱們徹底能夠自定義一個主題,覆蓋android.R.attr.fastScrollThumbDrawable屬性對應的Drawable不就搞定了!android
一、定義一個主題app
<style name="ListViewFastScrollThumb" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen"> <item name="android:fastScrollThumbDrawable">@drawable/ic_launcher</item> </style>
二、當前ListView所在Activity應用自定義的主題ide
<activity android:name="com.example.actionbardemo.MainActivity" android:label="@string/app_name" android:theme="@style/ListViewFastScrollThumb" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
三、驗證
佈局
public class MainActivity extends ListActivity { private static final int[] ATTRS = new int[] { android.R.attr.fastScrollThumbDrawable, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings)); ImageView imageView = (ImageView) findViewById(R.id.fastScrollDrawable); Theme theme = getTheme(); TypedArray a = theme.obtainStyledAttributes(ATTRS); Drawable drawable = a.getDrawable(0); imageView.setBackgroundDrawable(drawable); } }
佈局:this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/fastScrollDrawable" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:fastScrollEnabled="true" /> </LinearLayout>
Demo下載地址:http://download.csdn.net/detail/xyang81/6788411spa