8

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

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>

每件事都运行良好,我在首选项活动中的复选框之间自定义分隔符:

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

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

  list.setPadding(0, 0, 0, 0);

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

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

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

4

4 回答 4

4

允许首选项分隔符填充

首先:将此行添加到您的偏好活动中,这会导致 android 默认分隔符的透明度

list.setDivider(new ColorDrawable(0x00000000));

第二:在 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>

第三

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

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

希望对你有所帮助。

输出如下:

在此处输入图像描述

于 2013-08-06T01:38:38.740 回答
1

You should try deriving a theme from android's default one, assigning it to your preference screen (although they say there's a bug on 2.x versions that prevents you from doing that unless you workaround as described here), and overriding the android:dividerPadding property of one of the styles.

I can't really tell you for sure which style that would be, you'll have to dig into it, here's android's styles.xml for reference and here's an example how to override ListView appearance in a theme

于 2013-08-09T18:11:15.860 回答
1

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

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

在这种情况下,分隔线形状高度为 4dp,顶部填充为 5dp,当您设置分隔线高度时,您必须将形状高度 + 插图相加。

ListView list = (ListView) findViewById(android.R.id.list);
list.setDivider(new ColorDrawable(Color.RED)); // or some other color int
list.setDividerHeight((5)); //wrorng code
list.setVerticalScrollBarEnabled(false);

要为所有 dencity 屏幕正确设置分隔线高度,您必须在 dp 中设置它,但默认数字“5”以像素(px)解释。

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (dps * scale + 0.5f);

有关 shape 和 inset 可绘制资源的更多信息,请使用 Android 文档可绘制资源

于 2013-08-08T15:03:49.673 回答
1

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

custom_divider.xml

<shape android:shape="rectangle">
    <solid android:color="@color/grey" />
    <corners android:radius="2dp" />
</shape>

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

活动列表.xml

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/custom_divider"
    ...
/>
于 2013-08-01T02:08:18.687 回答