Android--ListView滾動條樣式
當ListView的記錄超過4頁時才會顯示滑塊
java代碼:
package com.example.test0123;
import java.lang.reflect.Field;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class TestList extends Activity {
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new ListAdapter());
try {
Field f = AbsListView.class.getDeclaredField("mFastScroller");
f.setAccessible(true);
Object o = f.get(lv);
f = f.getType().getDeclaredField("mThumbDrawable");
f.setAccessible(true);
Drawable drawable = (Drawable) f.get(o);
drawable = getResources().getDrawable(R.drawable.ic_launcher);
f.set(o, drawable);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public class ListAdapter extends BaseAdapter {
public int getCount() {
return 200;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(TestList.this);
tv.setTextSize(30);
tv.setText("aaaaa" + position);
return tv;
}
}
}