1

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

也不工作,

更新:

我的布局.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>

首选项.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);
    }
}

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

4

2 回答 2

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())。

于 2013-07-28T03:42:51.757 回答
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;
}
于 2019-11-19T14:03:33.070 回答