android设置首选屏幕的分隔符填充

android设置首选屏幕的分隔符填充,android,android-preferences,Android,Android Preferences,我有PreferenceScreen包含许多复选框,我通过将其引用到自定义布局进行自定义,如下所示: <?xml version="1.0" encoding="utf-8" ?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference android:summary="checkbox one" and

我有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="key_one" 
   android:layout="@layout/mylayout"      
    /> 
  <CheckBoxPreference 
   android:summary="checkbox two"
   android:key="key_two" 
   android:layout="@layout/mylayout"      
    /> 
</PreferenceScreen>
当我只想将分隔符的填充设置为:

  list.setPadding(0, 0, 0, 0);
它将填充设置为所有视图

如何将填充设置为仅分隔符而不影响所有视图的填充


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

您必须创建自定义可绘制形状

自定义分割器.xml

<shape android:shape="rectangle">
    <solid android:color="@color/grey" />
    <corners android:radius="2dp" />
</shape>
<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/custom_divider"
    ...
/>


将此设置为xml中ListView的分隔符

活动列表.xml

<shape android:shape="rectangle">
    <solid android:color="@color/grey" />
    <corners android:radius="2dp" />
</shape>
<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/custom_divider"
    ...
/>

允许首选项分隔符填充

首先:将这一行添加到您的首选项活动中,这将提高android默认分隔符的透明度

list.setDivider(new ColorDrawable(0x00000000));
second:在res中创建名为drawable的文件夹,然后在其上创建divider.xml,如下所示:

<?xml version="1.0" encoding="utf-8" ?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
   <solid android:color="#B22222" /> 
 </shape>
<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"
        />
</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" />

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

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

第三

将视图添加到mylayout.xml,使其如下所示:

<?xml version="1.0" encoding="utf-8" ?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
   <solid android:color="#B22222" /> 
 </shape>
<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"
        />
</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" />

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

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

此处的要点是在文本下方添加视图,并将该视图引用到可绘制res中的分隔器形状,因此最终您将获得一个自定义分隔器,可以根据需要进行自定义

希望这对你有帮助

输出如下:


您必须为分割器创建自己的可绘制资源:

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

<inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetTop="5dp">

    <shape android:shape="rectangle">

         <solid android:color="#FF0000" />

         <size android:height="4dp" />

    </shape>

</inset>
要为所有密度屏幕正确设置分隔高度,必须在dp中设置,但默认数字“5”以像素(px)表示


有关shape和inset drawable resource use Android Documentation的更多信息,请尝试从Android的默认主题派生主题,并将其分配到您的首选屏幕(尽管他们说2.x版本中存在一个bug,除非您按照说明解决此问题),否则无法执行此操作,并覆盖其中一种样式的
android:dividerPadding
属性


我不能确切地告诉你那将是哪种风格,你必须深入研究,这里是android供参考,这里是一个示例

我没有listview,我们在活动中将listview称为android.R.id.list,以允许在首选项屏幕中自定义分隔符,谢谢,我正在尝试删除首选项片段中的分隔线。但是我不知道如何调用
ListView list=(ListView)findviewbyd(android.R.id.list)我没有片段中的视图对象