如何在PreferenceScreen中的CheckBoxPreference之间自定义分隔符

时间:2013-07-28 00:58:43

标签: android android-preferences

我让PreferenceScreen包含许多CheckBox,并将主题称为:

  <activity 
        android:name=".Prefs" 
        android:label="@string/app_name" 
        android:theme="@style/PreferencesTheme">

我想在CheckBox之间自定义分隔符,但我也不能为PreferenceScreen创建自定义布局:

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

 <CheckBoxPreference 
    android:summary="checkbox one" 
    android:key="splash" 
    android:layout="@layout/mylayout"      
     /> 
 <CheckBoxPreference 
    android:summary="checkbox two"
    android:key="splash_music" 
    android:layout="@layout/mylayout"      
     /> 
</PreferenceScreen>

我尝试在mylayout.xml中创建视图,但它不能用作:

  <View 
  android:id="@+id/divider" 
  android:background="@drawable/divider" 
  android:layout_width="match_parent" 
  android:layout_height="5dp" 
  android:layout_below="@+android:id/title" /> 

也在prefs_style.xml中:

  <item name="android:divider">@color/red_color</item>

也不起作用,

更新

mylayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:minHeight="?android:attr/listPreferredItemHeight" 
  android:gravity="center_vertical" 
  android:paddingRight="?android:attr/scrollbarSize">
<LinearLayout 
  android:layout_width="wrap_content" 
  android:layout_height="match_parent" 
  android:gravity="center" 
  android:minWidth="@dimen/preference_icon_minWidth" 
  android:orientation="horizontal">
<ImageView 
  android:id="@+android:id/icon" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_gravity="center" 
  android:minWidth="48dp" /> 
</LinearLayout>
<RelativeLayout 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginLeft="16dip" 
  android:layout_marginRight="8dip" 
  android:layout_marginTop="6dip" 
  android:layout_marginBottom="6dip" 
  android:layout_weight="1">
<TextView 
  android:id="@+android:id/title" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textAppearance="?android:attr/textAppearanceMedium" 
  android:fadingEdge="horizontal" /> 
<TextView 
  android:id="@+android:id/summary" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_below="@android:id/title" 
  android:layout_alignLeft="@android:id/title" 
  android:textAppearance="?android:attr/textAppearanceSmall" 
  android:textColor="?android:attr/textColorSecondary" 
  android:maxLines="4" /> 
</RelativeLayout>
 <!--  Preference should place its actual preference widget here. 

<LinearLayout 
  android:id="@+android:id/widget_frame" 
  android:layout_width="wrap_content" 
  android:layout_height="match_parent" 
  android:minWidth="@dimen/preference_widget_width" 
  android:gravity="center" 
  android:orientation="vertical" /> 
</LinearLayout>

prefs_style.xml

<?xml version="1.0" encoding="utf-8" ?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android">
 <style name="CustomWindowTitleBackground" /> 
 <style name="PreferencesTheme" parent="android:Theme">
<item name="android:windowTitleSize">35dp</item> 
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground
</item> 
<item name="android:background">#FFDAB9</item> 
<item name="android:textColorSecondary">@color/red_color</item> 
<item name="android:textSize">25sp</item> 
<item name="android:divider">@color/red_color</item> 
</style>
</resources>

Prefs.java

  public class Prefs extends PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    Boolean customTitleSupported =  
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    if (customTitleSupported) { 

         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 
         TextView tv = (TextView) findViewById(R.id.title_tv1); 
         tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
         tv.setText("Preference");  
         }
    addPreferencesFromResource(R.xml.prefs); 
    ListView lv = getListView();
    lv.setDivider(new ColorDrawable(Color.RED)); 
    lv.setDividerHeight(10);
    }
}

任何帮助将不胜感激,谢谢

2 个答案:

答案 0 :(得分:3)

试试这个:

ListView list = (ListView) findViewbyId(android.R.id.list);
list.setDivider(new ColorDrawable(Color.RED)); // or some other color int
list.setDividerHeight((int) getResources.getDisplayMetrics().density); // or some value > 0

在您调用xml加载首选项后执行此操作(因此可能在onCreate()结尾处。)

答案 1 :(得分:0)

如何在PreferenceFragment中实现跟踪方法

  @Override
public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

    RecyclerView recyclerView = super.onCreateRecyclerView(inflater, parent, savedInstanceState);
    DividerItemDecoration itemDecoration = new DividerItemDecoration(getContext(), RecyclerView.VERTICAL);
    itemDecoration.setDrawable(getContext().getDrawable(android.R.drawable.divider_horizontal_dark));
    recyclerView.addItemDecoration(itemDecoration);
    return recyclerView;
}