2016-04-20 76 views
1

我使用v7支持庫中的PreferenceFragmentCompat來顯示一些設置。我的preferences.xml文件如下 -如何在PreferenceFragment中顯示分隔線?

<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> 

Java代碼 -

public class SettingsFragment extends PreferenceFragmentCompat { 

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

我使用偏好的主題是 -

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="preferenceTheme">@style/AppTheme.Preference</item> 
</style> 

<style name="AppTheme.Preference" parent="@style/PreferenceThemeOverlay"> 
    <item name="android:textColorPrimary">@android:color/holo_blue_dark</item> 
</style> 

四個設置只顯示一個低於另一個,沒有任何分隔線。如何在兩個項目之間顯示分隔線?

回答

1

對於一些修訂,com.android.support:preference-v7支持庫使用RecyclerView以便立即顯示設置。開箱即用的RecyclerView沒有一個分隔符但添加ItemDecoration到RecyclerView將工作:

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> 
相關問題