更改PreferenceFragment的滚动条样式

时间:2018-06-07 21:03:11

标签: java android scrollbar preferencescreen

public class SettingsFragment extends PreferenceFragment implements
    OnSharedPreferenceChangeListener
{
      public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences); // Almost empty file
        //add a PrefrenceCategory
        //add multiple SwitchPreference to the category
    }  
}

的preferences.xml

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:persistent="false">

</PreferenceScreen>

我试过的方法:

将android:scrollbarThumbVertical =“@ drawable / scroller”放到PreferenceScreen

以编程方式将滚动条设置为onViewCreated中的视图,PreferenceCategory和PreferenceScreen,其中没有一个工作

    try
    {
        Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
        mScrollCacheField.setAccessible(true);
        Object mScrollCache = mScrollCacheField.get(preferenceScreen);
        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);
        method.invoke(scrollBar, ResourcesCompat.getDrawable(getResources(), R.drawable.scroller, null));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

0 个答案:

没有答案