如何在PreferenceFragment中显示分隔符?

时间:2016-04-20 10:23:13

标签: android android-support-library android-preferences

我正在使用v7支持库中的<condition property="sw.root" value="c:\flag"> <os family="windows" /> </condition> <condition property="sw.root" value="/opt/flag"> <os family="unix" /> </condition> <property name="sw.root" value="/os/unknown/"/> 来显示一些设置。我的preferences.xml文件如下 -

PreferenceFragmentCompat

Java代码 -

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <EditTextPreference
        android:defaultValue="@string/temp_url_default"
        android:key="temp_edit_preference_key"
        android:title="@string/temp_url_setting_title" />

    <EditTextPreference
        android:defaultValue=""
        android:key="username_preference_key"
        android:title="@string/username_setting_title" />

    <EditTextPreference
        android:defaultValue=""
        android:key="password_preference_key"
        android:title="@string/password_setting_title" />

    <ListPreference
        android:key="reset_preference_key"
        android:title="@string/reset_setting_title" />

</PreferenceScreen>

我用于偏好的主题是 -

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.preferences);
    }
}

这四个设置只是一个在另一个下面显示,中间没有任何分隔符。如何在两个项目之间显示分隔符?

1 个答案:

答案 0 :(得分:1)

对于一些修订,com.android.support:preference-v7支持库使用RecyclerView以便立即显示设置。 RecyclerView开箱即用没有分隔符,但在RecyclerView中添加ItemDecoration将起作用:

public class YourPreferenceFragment extends PreferenceFragmentCompat {

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recyclerView = (RecyclerView) getView().findViewById(R.id.list);
        recyclerView.addItemDecoration(
                new DividerItemDecorationPreferences(
                        getActivity(),
                        getResources().getDimensionPixelSize(R.dimen.row_preferences_margin_horizontal),
                        getResources().getDimensionPixelSize(R.dimen.row_preferences_margin_horizontal)));
    }

}

显然你可以使用你喜欢的任何分频器。至于我自己使用以下课程:

public class DividerItemDecorationPreferences extends RecyclerView.ItemDecoration {

    private Drawable mDivider;
    private int paddingLeft = 0;
    private int paddingRight = 0;

    public DividerItemDecorationPreferences(Context context, int paddingLeft, int paddingRight) {
                mDivider = ContextCompat.getDrawable(context, R.drawable.divider_recycler_view);
        this.paddingLeft = paddingLeft;
        this.paddingRight = paddingRight;
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = paddingLeft;
        int right = parent.getWidth() - paddingRight;
        int childCount = parent.getChildCount();
        boolean lastIteration = false;
        for (int i = 0; i < childCount; i++) {
            if (i == childCount - 1)
                lastIteration = true;
            View child = parent.getChildAt(i);
            if (!lastIteration) {
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                int top = child.getBottom() + params.bottomMargin;
                int bottom = top + mDivider.getIntrinsicHeight();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }

}

<强> divider_recycler_view.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size android:height="1dp" />
    <solid android:color="#1F000000" />

</shape>