改变编程方式滚动型的颜色 [英] Change the Color of ScrollView Programmatically
问题描述
我目前在做什么
目前,我使用的<一个已经改变了滚动条在我的XML文件href="http://developer.android.com/reference/android/view/View.html#attr_android%3ascrollbarThumbVertical"><$c$c>android:scrollbarThumbVertical$c$c>像这样属性:
&LT;滚动型
机器人:ID =@ + ID / scrollView1
机器人:scrollbarThumbVertical =@可绘制/ scrollbar_blue
...&GT;
和 scrollbar_blue
指的是我的 scrollbar_blue.xml
文件,该文件是这样的:
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;形状的xmlns:机器人=http://schemas.android.com/apk/res/android&GT;
&LT;梯度
机器人:角=45
机器人:centerColor =@色/蓝色
机器人:endColor =@色/蓝色
机器人:startColor =@色/蓝色/&GT;
&LT;边角机器人:半径=8DP/&GT;
&LT; /形状&GT;
我想做什么
我有我的应用程序色彩的选择 - 所以当颜色上,它应该保持蓝色;否则,它应该是灰色。
如何以编程方式(在我的活动课)改变我的滚动型使用我的 scrollbar_grey.xml
如果你看一下在<一个Android的文件href="http://developer.android.com/reference/android/widget/ScrollView.html"><$c$c>ScrollView$c$c>,没有相应的方法安卓scrollbarThumbVertical
我很好用另一种方式改变颜色为好。
下面是我如何创建引用到我的滚动型:
滚动型SCR =(滚动型)findViewById(R.id.scrollView1);
有一种方法以编程方式改变它,但是这种方法不外露。似乎没有为别的,就从我已阅读编程方式更改它。
不过,我也碰到过,使用反射做这个计算器的答案。 请upvote的答案,如果你的作品:<一href="http://stackoverflow.com/a/19819843/3286163">http://stackoverflow.com/a/19819843/3286163
答案是一个列表视图,但同为滚动视图:
滚动型SCR =(滚动型)findViewById(R.id.scrollView1);
尝试
{
现场mScrollCacheField = View.class.getDeclaredField(mScrollCache);
mScrollCacheField.setAccessible(真正的);
对象mScrollCache = mScrollCacheField.get(SCR); // SCR是你的滚动视图
现场scrollBarField = mScrollCache.getClass()getDeclaredField(滚动条)。
scrollBarField.setAccessible(真正的);
对象滚动条= scrollBarField.get(mScrollCache);
。方法方法= scrollBar.getClass()getDeclaredMethod(setVerticalThumbDrawable,Drawable.class);
method.setAccessible(真正的);
//这里设置你的绘制。
method.invoke(滚动条,getResources()getDrawable(R.drawable.scrollbar_blue));
}
赶上(例外五)
{
e.printStackTrace();
}
我唯一能找到的。我给它一个尝试自己和它的工作。
What I'm currently doing
Currently, I have changed the scrollbar in my XML file using the android:scrollbarThumbVertical
property like so:
<ScrollView
android:id="@+id/scrollView1"
android:scrollbarThumbVertical="@drawable/scrollbar_blue"
... >
And scrollbar_blue
refers to my scrollbar_blue.xml
file, which is this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="45"
android:centerColor="@color/blue"
android:endColor="@color/blue"
android:startColor="@color/blue" />
<corners android:radius="8dp" />
</shape>
What I want to do
I have a colour option for my app - so when the colour is on, it should stay blue; otherwise, it should be grey.
How can I programmatically (in my activity class) change my ScrollView to use my scrollbar_grey.xml
?
If you look at the Android documentation on ScrollView
, there is no corresponding method to android:scrollbarThumbVertical
I'm fine with another way change the colour as well.
Here is how I create the reference to my ScrollView:
ScrollView scr = (ScrollView)findViewById(R.id.scrollView1);
There is a method to change it programmatically but that method is not exposed. There doesn't seem to be anything else to change it programmatically from what I have read.
However, I did come across this one stackoverflow answer that uses reflection to do it. Please upvote the answer there if it works for you: http://stackoverflow.com/a/19819843/3286163
The answer was for a listview but is the same for the scrollview:
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();
}
Only thing I could find. I gave it a try myself and it worked.
这篇关于改变编程方式滚动型的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!