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

and*_*qq6 8 android android-preferences

我有PreferenceScreen包含许多CheckBox,我通过引用自定义布局自定义它:

<?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>
Run Code Online (Sandbox Code Playgroud)

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" /> 
</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>
<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>
Run Code Online (Sandbox Code Playgroud)

每个东西运行正常我也在prefs活动中的复选框之间自定义divider:

 ListView list = (ListView) findViewById(android.R.id.list);
    list.setDivider(new ColorDrawable(Color.RED)); // or some other color int
    list.setDividerHeight((5)); 
    list.setVerticalScrollBarEnabled(false);
Run Code Online (Sandbox Code Playgroud)

当我想将填充仅设置为分隔符时:

  list.setPadding(0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

它将填充设置为所有视图,

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

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

And*_*ack 4

允许首选项分隔线填充

首先:将此行添加到您的首选项活动中,这会导致 Android 默认分隔线透明

list.setDivider(new ColorDrawable(0x00000000));
Run Code Online (Sandbox Code Playgroud)

第二:在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>
Run Code Online (Sandbox Code Playgroud)

第三

将 View 添加到 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"
        />
</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" />
Run Code Online (Sandbox Code Playgroud)

在这里,您在文本下方添加视图,并将该视图引用到可绘制资源中的分隔线形状,因此最终您将获得一个自定义分隔线,可以根据需要对其进行自定义。

希望对您有帮助。

输出如下:

在此输入图像描述