【问题标题】:android set divider padding for preference screenandroid为偏好屏幕设置分隔线填充
【发布时间】:2013-08-01 01:06:49
【问题描述】:

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

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

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

它为所有视图设置填充,

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

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

【问题讨论】:

    标签: android android-preferences


    【解决方案1】:

    允许偏好分隔符填充

    first:将此行添加到您的偏好活动中,这会导致 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>
    

    第三个

    将视图添加到您的 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 中的分隔线形状,因此最终您将获得一个自定义分隔线,可以根据需要对其进行自定义。

    希望对你有所帮助。

    输出如下:

    【讨论】:

    • 嗨,我正在尝试删除我的偏好片段中的分隔线。但我不知道如何调用ListView list = (ListView) findViewById(android.R.id.list); 我在片段中没有视图对象
    【解决方案2】:

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

    custom_divider.xml

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

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

    activity_list.xml

    <ListView
        android:dividerHeight="2dp"
        android:divider="@drawable/custom_divider"
        ...
    />
    

    【讨论】:

    • 我没有 listview ,我们在活动中将 listview 称为 android.R.id.list 以允许在偏好屏幕中自定义分隔线,谢谢
    【解决方案3】:

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

    <?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 文档Drawable resources

    【讨论】:

      【解决方案4】:

      您应该尝试从 android 的默认主题派生一个主题,将其分配给您的偏好屏幕(尽管他们说 2.x 版本有一个错误阻止您这样做,除非您按照here 描述的解决方法),然后覆盖一种样式的android:dividerPadding 属性。

      我不能确切地告诉你那会是哪种风格,你必须深入研究它,这是 android 的 styles.xml 供参考,这是一个示例 how to override ListView appearance in a theme

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-27
        相关资源
        最近更新 更多