关于Android 控件没有动态设置界面效果时,如何处理
例子:android:scrollbarThumbVertical 动态设置 -->listView
如果控件只有静态xml界面的设置,没有动态设置时,需要去查看安卓控件的源码,看该控件继承了那个父控件的方法,然后利用反射调用!
不同的Android 版本,源码可能不同!
Android 11.0
View.java
/**
* Defines the vertical scrollbar thumb drawable
* @attr ref android.R.styleable#View_scrollbarThumbVertical
*
* @see #awakenScrollBars(int)
* @see #isVerticalScrollBarEnabled()
* @see #setVerticalScrollBarEnabled(boolean)
*/
public void setVerticalScrollbarThumbDrawable(@Nullable Drawable drawable) {
initializeScrollBarDrawable();
mScrollCache.scrollBar.setVerticalThumbDrawable(drawable);
}
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:entries="@array/array_adapter_strs" />
//ListView 动态改变scrollbarThumbVertical
private void changeViewProgress(){
try {
Class three = Class.forName("android.view.View");
Method method_2 = three.getMethod("setVerticalScrollbarThumbDrawable", Drawable.class);
method_2.invoke(listView,getDrawable(R.drawable.title_music));
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
标签:控件,界面,printStackTrace,设置,Android,动态,drawable
From: https://www.cnblogs.com/kato-T/p/17568457.html