更新自定义ScrollBar UI的颜色

时间:2016-10-10 00:09:49

标签: java swing user-interface jscrollpane

如何使用自定义BasicScrollBarUI重新设置ScrollBar的颜色?

我知道我可以用它来第一次设置颜色:

Import

但是,构造函数调用了这个,我无法再手动调用它。

我需要滚动条的颜色来改变何时和动作触发。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我不确定你需要什么但请告诉我这是否有帮助

ScrollView scr = (ScrollView)findViewById(R.id.scrollView1);
try
{
Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
mScrollCacheField.setAccessible(true);
Object mScrollCache = mScrollCacheField.get(scr); // scr is your Scroll View

Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
scrollBarField.setAccessible(true);
Object scrollBar = scrollBarField.get(mScrollCache);

Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
method.setAccessible(true);

// Set your drawable here.
method.invoke(scrollBar, getResources().getDrawable(R.drawable.scrollbar_blue));
} catch(Exception e) {
e.printStackTrace();
}

listview.mScrollCache.scrollBar.setVerticalThumbDrawable(getResources().getDrawable(R.drawable.scrollbar_style));