android:scrollbarThumbVertical与 android:scrollbarTrackVertical动态设置


        <ScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="440dp"
            android:paddingLeft="40dp"
            android:paddingRight="26px"
            android:paddingTop="40dp"
            android:scrollbarThumbVertical="@drawable/custom_scrollbar_thumb"
            android:scrollbarTrackVertical="@drawable/custom_scrollbar_track"
            android:fadeScrollbars="false">

源码没有对外提供接口,在源码中找到方法,android:scrollbarThumbVertical与 android:scrollbarTrackVertical动态设置_第1张图片
反射

    private void changeScrollViewProgress(View view, Drawable thumb, Drawable track) {
        try {
            Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
            mScrollCacheField.setAccessible(true);
            Object mScrollCache = mScrollCacheField.get(view);
            Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
            scrollBarField.setAccessible(true);
            Object scrollBar = scrollBarField.get(mScrollCache);
            Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
            Method method1 = scrollBar.getClass().getDeclaredMethod("setVerticalTrackDrawable", Drawable.class);
            method.setAccessible(true);
            method1.setAccessible(true);
            method.invoke(scrollBar, thumb);
            method1.invoke(scrollBar, track);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(android,java,开发语言)