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

2023-11-24

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

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

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

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


允许首选项分隔线填充

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>

third :

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

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

希望对您有帮助。

输出如下:

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android 设置首选项屏幕的分隔线填充 的相关文章

随机推荐

  • 按值对对象属性进行排序

    如果我有一个 JavaScript 对象 例如 var list you 100 me 75 foo 116 bar 15 有没有办法根据值对属性进行排序 所以我最终得到 list bar 15 me 75 you 100 foo 116
  • Akka (2.3.0) 无法加载 Slf4jEventHandler 类并出现 java.lang.ClassNotFoundException

    我从 Akka 2 2 3 迁移到 2 3 0 RC4 并在应用程序启动时收到此错误消息 error while starting up loggers akka ConfigurationException Logger specifie
  • 使用node.js提示用户输入? [复制]

    这个问题在这里已经有答案了 我正在开发一个使用 node js 运行的 JS 项目 但我不知道如何让提示正确地用于用户输入 我从 npm 安装了它 按照步骤操作 我可以让程序提示用户输入 但无法将结果存储在变量中 我想要的是提示用户每回合的
  • 如何从自定义的QMessageBox中捕获按钮点击?

    如何修改下面自定义 QMessageBox 的代码才能知道用户是否单击 是 或 否 from PySide import QtGui QtCore Create a somewhat regular QMessageBox msgBox Q
  • 没有为带有 ARC 的 MKMapView 释放内存

    我有一个习惯UIView called ActivityDetailView我实例化然后添加到父视图控制器内的滚动视图 当分配此自定义视图时 每次额外的内存都会占用大约 1mb 并且 Instruments 显示内存永远不会被释放 即使视图
  • 欧拉到矩阵以及矩阵到欧拉的转换

    我正在尝试使用 NET C 将欧拉角描述的 3D 旋转转换为矩阵 然后再转换回来 我的约定是 左手系统 x 向右 y 向上 z 向前 旋转顺序 绕 y 航向 绕 x 俯仰 绕 z 倾斜 使用左手定则旋转为正 拇指指向 无穷大 我的试用是 欧
  • Magento:覆盖客户帐户控制器

    您好 我正在尝试覆盖 Mage Customer AccountController 以便我可以扩展 createPostAction 方法 对于我的一生 我似乎无法做到这一点 它要么抛出一个 404 页面 这表明它不是文件的正确路径 要么
  • Java - 需要一个记录堆栈跟踪的日志包

    是否有一个记录器可以轻松记录我的堆栈跟踪 我得到的ex printStackTrace 我搜索了 log4j 文档但什么也没找到 关于记录堆栈跟踪 我可以自己做这个 StringWriter sw new StringWriter ex p
  • 如何防止exoplayer在向后搜索时重新加载?

    我正在使用 exoplayer 播放 mp3 曲目 都好 如果轨道完全缓冲 那么在向前查找的情况下 它不会按预期重新加载 但是 如果向后查找 它会重新加载 我该如何防止这种情况 是exo的bug吗 这并不是一个真正的解决方案 但您可以使用一
  • 如何获得第二个System.Thread.ThreadPool?

    如果我以嵌套方式使用 ThreadPool 我的应用程序将挂起 ThreadPool QueueUserWorkItem state gt ThreadPool QueueUserWorkItem Action 如何获得第二个独立的Thre
  • Eq 和 Ord 实例不一致?

    我有一个大型 Haskell 程序 它运行速度慢得令人沮丧 分析和测试表明 很大一部分时间都花在比较特定大型数据类型的相等性和顺序上 这非常重要 相等是一个有用的操作 这是状态空间搜索 图搜索比树搜索更可取 但我只需要此类的 Ord 实例即
  • 元素的类名中包含(方)括号的原因是什么?

    我对这个说法感到非常惊讶class col xs 12 col sm offset 1 col sm 5 在 bootstrap 3 模板中 我不确定两者之间有什么区别 class col xs 12 col sm offset 1 col
  • sails-mysql 架构数据类型

    任何人使用节点的 sails 框架使用 mysql 作为数据库 https github com balderdashy sails mysql 我陷入模型中 无法创建数据库结构 我需要用来创建架构的数据类型不起作用 我到处搜索了一些文档
  • 用于匹配不同 Unicode 脚本之间边界的正则表达式

    正则表达式引擎有一个 零宽度 匹配的概念 其中一些对于查找单词的边缘很有用 b 存在于大多数引擎中 以匹配单词和非单词字符之间的任何边界 lt and gt 存在于 Vim 中分别仅匹配单词开头和结尾的边界 一些正则表达式引擎中的一个较新的
  • 使用脚本启用与 sql express 的远程连接

    我正在使用 sql server express 2008 部署一个应用程序 在我的应用程序的先决条件部分中 我已包括 因此 当用户安装我的应用程序时 它也会安装 sql express 然后我将能够连接到该数据库引擎 try databa
  • 读取 MIDI 文件

    读取具有多个轨道的 MIDI 文件 按时间顺序 的最佳方法是什么 爪哇 注意 我不想播放 MIDI 文件 只想读取消息 情侣想法 是否可以安全地假设没有比 1 64 音符短的音符事件 或者我应该访问每个轨道并仅在所有其他刻度线之后移动到下一
  • 本机 C# pdf 阅读器 [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 目前不接受答案 我需要从 PDF 文件中提取文本 我找到了 iTextSharp 和 PDFBox 但它们都只是 Java 端口 为了使它们工作 我需要使用大量的附
  • CSS 背景图像在 iPhone 上的渲染方式不同

    我们的页面设计在我尝试过的所有 PC 浏览器中都表现良好 但在 iPhone 或 iPod Touch 上查看时却很奇怪 问题与非常高的居中背景图像有关 content container background image url cont
  • 在 log4net 中使用 smtpAppender 的多个 smtphost 地址

    我希望能够指定多个 smtp 服务器主机地址并实现一种逻辑 如果使用一个 smtp 服务器的电子邮件失败 它会尝试使用下一个 smtp 服务器地址发送 是否可以使用 log4net 我们可以重写 log4net 的一些功能并在其中实现我们自
  • android 设置首选项屏幕的分隔线填充

    我有 PreferenceScreen 包含许多 CheckBox 我通过将其引用到自定义布局来自定义它 如下所示