Android 自定义 Preference

android8.0 Settings之自定义各种Preference控件 以前使用android.preference包里面的类创建出来的偏好设置界面是没有兼容低版本的,后来发现com.android.support:preference库有对低版本做兼容,在低版本上面也可以有Material Design风格,于是对其作了一番研究。 1、应用的主题需要继承自Theme.AppCompat系列: 2、在build.gradle中添加以下依赖: compile ‘com... 阅读详情

有些时候系统提供的 Preference 不满足我们的要求的时候,我们就需要自己定制了。现在产品要求 ChekBoxPreference? 的 summary 的颜色要能动态改变,在关闭的时候是默认颜色,在开启的时候变成红色。现在我们就可以自己定制啦。

简单的修改 xml

先说说简单的情况。如果字体颜色只是静态的话,可以不用改代码,改改 layout xml 就好了。系统自己的 xml 是 framework/base/core/res/res/layout/preference.xml 。把这个文件复制,然后在其基础上改改 text 的属性就好了,然后在使用  CheckBoxPreference?  的时候指定自己定制的 xml(这里我叫 custom_preference.xml)。
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!-- Layout for a Preference in a PreferenceActivity. The
Preference is able to place a specific widget for its particular
type in the "widget_frame" layout. -->
<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">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip"
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:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />

// 就是在这里改啦,加一个 textColor 属性
<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="#FFFF0000" 
android:maxLines="4" />

</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:gravity="center_vertical"
android:orientation="vertical" />

</LinearLayout>

// 改好后,在用 CheckBoxPreference 的时候,指定自己的 xml layout
<CheckBoxPreference android:key="lock_screen" 
android:title="@string/settings_lock_screen_title" 
android:defaultValue="false" 
android:layout="@layout/custom_preference"
android:summaryOff="@string/settings_lock_screen_off_summary" 
android:summaryOn="@string/settings_lock_screen_on_summary" 
android:summary="@string/settings_lock_screen_off_summary">
</CustomCheckBoxPreference>

扩展代码

如果需要复杂点的功能,就需要继承  CheckBoxPreference?  ,然后自己定制代码了。这里以动态的改字体颜色为例:继承  CheckBoxPreference?  后,重载父类的 onBindView 函数(这个函数好像是在将数据显示到视图上的时候调用的):

public class CustomCheckBoxPreference extends CheckBoxPreference {
        
        public CustomCheckBoxPreference(Context context, AttributeSet attrs,
        int defStyle) {
                super(context, attrs, defStyle);
                // TODO Auto-generated constructor stub
        }
        
        @Override
        protected void onBindView(View view) {
                super.onBindView(view);
                
                boolean isChecked = isChecked();
                Resources res = view.getResources();
                
                TextView summaryView = (TextView)view.findViewById(com.android.internal.R.id.summary);
                if (summaryView != null) {
                        if (isChecked) {
                                summaryView.setTextColor(res.getColor(R.color.red));
                        } else {
                                //summaryView.setTextAppearance(getContext(), com.android.internal.R.attr.textAppearanceSmall);
                                summaryView.setTextColor(res.getColor(com.android.internal.R.color.secondary_text_light));
                        }
                }
        }
}

写好自己的类之后,在用的时候把原来的 CheckBoxPreference? 换成自己的类就可以了:

<cn.fmsoft.launcher2.CustomCheckBoxPreference android:key="lock_screen" 
android:title="@string/settings_lock_screen_title" 
android:defaultValue="false" 
android:summaryOff="@string/settings_lock_screen_off_summary" 
android:summaryOn="@string/settings_lock_screen_on_summary" 
android:summary="@string/settings_lock_screen_off_summary">
</cn.fmsoft.launcher2.CustomCheckBoxPreference>

参考

参考此处: Preference 使用小结
android 自定义Preference CheckBoxPreference ListPreference的用法 自定义Preference类 package com.pne.view; import android.content.Context; import android.preference.Preference; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget 阅读详情

相关推荐

Android 自定义preference组件

上篇博文代码实现了Android自带的preference组件,本文将通过实例讲解自定义preference组件。 主要通过以下几步来实现: 1.定义需要的layout布局res->layout->xml文件; 2.通过继承Preference类,来实现自定义preference组件类; 3.通过xml文件引用自定义preference组件,res->xml->xml文件; 下面通过实例来实现: 1.需求:本实例主要实现一个LinearLayout包含ImageView,Button,TextView,通过点击LinearLayout,ImageView,Button,TextView中显示相关的内容;

Preference 定义字体颜色

使用PreferenceScreen的时候,有时会faxia PreferenceScreen 里的preference 无法直接定义颜色

yxdspirit的专栏 1948

ElasticSearch集群搭建

docker安装es集群

weixin_46562226的博客 2024

Android Preference修改点击条目的背景色

很多朋友在用Preference时,都觉得这个控件好用,但是很多的东西不能自己定制,比如字体大小,背景色,点击背景色等等,大家都知道这个控件是放在framework中的,总不能为了一个小需求就去改动framework的代码吧,本文就以修改点击背景色为例,提供一种在不改动framework的前提下定制自己风格的Preference的思路,首先,我们先来看看Preference的源码, 在路径plat

baidu_20792589的博客 5244

VisualSFM+MeshLab 三维重建全过程记录

VisualSFM+MeshLab 三维重建全过程记录,从下载到运行出结果,以及其中一些可能出现的小问题。通过这两个软件熟悉一下多目立体视觉重建的流程。

何似莫相逢 3万+

RAG-Anything 实战:构建企业级多模态知识图谱问答系统

本文详细介绍了如何利用RAG-Anything构建企业级多模态知识图谱问答系统,解决跨模态文档处理的痛点。通过架构设计解析、部署实战指南和典型业务场景解决方案,帮助企业实现高效的知识管理和智能检索,提升决策效率。

weixin_29089057的博客 385

Android自定义PreferenceCategory的title颜色、字体大小写的方法

本文讲述了Android设置PreferenceCategory背景颜色的方法。分享给大家供大家参考。(参考文章 Android设置PreferenceCategory背景颜色) 具体分析如下: 大家可能遇到,PreferenceCategory默认是黑色背景,而且默认情况下PreferenceCategory的title会将英文字母全部转为大写,看起来很不协调,那么怎么更改背景呢?我们可以通

codetyper的博客 8305

scRNA-seq的fastq文件处理流程——持续更新

获得了一批新的scRNA-seq的fastq.gz文件,每个样本的数据形式为: 我打算使用10X cellranger处理fastq.gz文件,下载及安装方式在官网:https://support.10xgenomics.com/single-cell-gene-expression/software/downloads/2.0这个网址写了10X cellranger处理fastq.gz文件的命名规则https://support.10xgenomics.com/single-cell-gene-expr

watermel__的博客 3198

Preference-自定义PreferenceCategory

1. Preference-自定义PreferenceCategory 修改字体大小、样式、颜色 2. 参考代码 package com.android.fadi.powersave.view; import android.content.Context; import android.graphics.Color; import android.graphics.Typeface; impo...

苏法迪的专栏 5295

Andriod应用开发基础项目——记账本

本项目采用Java语言编写,是一款基于Android Studio开发的简易项目,本项目可以提高Java语言编写软件的能力,同时加深对开发软件Android studio有了更深层次的掌握,很适合初学者。在项目过程中,遇到了一些挑战和问题,但通过不断的努力和学习,最终取得了成功。未来,可以还可以继续优化App的功能和性能,增加更多实用的功能,提升用户体验。

eryasd的博客 3467

新能源汽车电机交流电流测量系统精度全链路分析

通过全链路误差建模与瓶颈分析,结合硬件选型优化与软件算法补偿,可将系统精度从4.2%提升至≤1% FS。本文所述方法论已应用于某800V电驱平台开发,实测电流控制精度达±0.8%,为高性能电机控制奠定基础。为例,深入剖析从互感器采集到CAN报文解析的全链路信号处理流程,揭示各环节误差来源及对最终精度的影响,为研发测试提供理论支撑。:±0.1%(电阻温漂与运放开环增益限制):运放增益带宽积(GBW)不足导致高频衰减。:±0.02%/℃(铁氧体材料温敏特性):±0.5%读数(磁芯饱和效应)

优快云-新能源汽车研发测试专业博主 1492

android preference-headers 字体颜色,android 批改preference中view属性(title,summary字体颜色等)...

当前位置:我的异常网» Android»android 批改preference中view属性(title,summary字android 批改preference中view属性(title,summary字体颜色等)www.myexceptions.net网友分享于:2013-03-26浏览:286次android 修改preference中view属性(title,summary字...

weixin_39860108的博客 586

YOLOv13优化Faster_Block:卷积(Partial Convolution)、多层感知机(MLP)结构和残差连接

Faster_Block 代表了现代深度学习中效率与性能平衡的一个典型案例。它通过部分卷积技术巧妙地减少了计算开销,同时通过精心设计的架构保持了强大的特征学习能力。这种设计思想对于移动端部署、实时应用和大规模模型训练都具有重要意义。 未来的发展方向可能包括更智能的动态分割策略、与新兴硬件的深度适配,以及与其他先进技术(如自注意力机制、神经架构搜索等)的结合。Faster_Block 的成功展示了在深度学习领域,创新往往来自于对问题本质的深刻理解和巧妙的工程实现。

FJN110的博客 427

Android preference 自定义控件

preference中只提供几种配置控件,远远达不到我们程序的要求,最近做项目中需要一个时间控件,配置如下: 1.自定义控件的布局文件,只是简单放一个TimePicker控件   xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:

android 开发专栏 2733

手把手教你学PCIE(9.3)--pcie常见故障及原因:配置错误介绍

PCIe 设备的配置错误可能涉及操作系统设置、BIOS 设置、驱动程序设置和配置文件设置等多个方面。通过以上方法,可以有效地诊断和解决 PCIe 设备的配置错误。

MHD0815的博客 2436

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Android 自定义Preference 讲解

Andorid 自定义Preference 详细讲解

u012514113的专栏 4187

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

android preference自定义

1、对于一些简单的preference布局可以仿照frameworks\base\core\res\res\layout文件夹下面的preference相关的布局文件进行布局,但是在书写的时候要注意关于每一个id的名称一定要使用preference系统文件里面的,不可以自己定义,否则会出现异常 2、android中自定义preference,自定义的流程是:首先是在onCreateV

robot的专栏 1万+

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Android自定义控件——自定义Preference

如图定义一个颜色选择的Preference 思路:继承Preference,重写onCreateView 前提:需要一个布局文件,TextView显示文本,ImageView显示颜色 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pref

顾明伟 5304

preference android:layout,Android设置选项开发及自定义Preference样式

2本文引用地址:http://www.eepw.com.cn/article/201609/304509.htm34567复制代码2) 设计自定义Preference的布局 preferencewithtip.xml123 android:layout_width=match_parent4 android:layout_height=match_parent5 android:orientati...

weixin_42125770的博客 423

Android自定义Preference点击波纹

在安卓设计开发中,设置页面是比较常见的设计场景,Android Jetpack 为我们提供了Preference来便于我们快速构建设置页面,本文主要来讲解如何通过替换资源文件来实现自定义Preference点击波纹。

人生就是为了樱岛麻衣的博客 1234

Android(12)自定义 Preference(二)

Android(12)自定义 Preference(二) 简单的效果效果演示: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-apgIG18e-1643643122163)(C:\Users\Yansw1\Desktop\效果演示1.gif)] 使用到的控件(原生): CoordinatorLayout CollapsingToolbarLayout AppBarLayout Toolbar NestedScrollView FragmentContainerView 准备

C_biubiubiu的博客 3503

android 自定义preference的属性

最近碰到了个问题,我想调整checkboxpreference中的图片的大小以及位置,学习了下发出来: 1. 拷贝并重命名framework/base/core/res/res/layout/preference.xml到你的工程目录res/layout/,如mypreference.xml 2. 修改mypreference中你想修改对应的属性,不赘述,大家都会。 3. 如果是用静态加载p

shadow_dance的专栏 4716

android 自定义设置界面,Android 设置界面之 Preference

Android系统为设置界面的UI提供了一系列的接口,设置界面的部分和Activity是分离的,会有一个PreferenceScreen的对象是根目录,在其中会包含CheckBoxPreference EditTextPreference ListPreference PreferenceCategory RingtonePreference相关的接口Preference.OnPreference...

weixin_39609503的博客 1042

android自定义layout布局,Android布局——Preference自定义layout的方法

导语:preferenceactivity是一个方便设置管理的界面,但是对于界面显示来说比较单调,所以自定义布局就很有必要了。本文举例说明在preference中自定义layout的方法。笔者是为了在设置中插入@有米v4广告条才研究了一晚上的。正文:首先preferencescreen是一个xml文件于res/xml目录下,不属于layout文件。要插入layout,有两种方法。1.使用prefe...

weixin_39548193的博客 511

android 自定义switchpreference,Android设置选项开发及自定义Preference样式

一个完整的Android应用程序都应该提供选项(或者叫偏好设置等等)让用户对APP的表现形式能够进行设置,比如说是否加入用户体验计划,或者是否自动升级、定时提醒、开启自启动、后台运行等等。提供一个好的设置项,会大大提升APP的用户体验。为了完成这样的功能,你不必从头开始写Activity或者Fragment,因为Android已经提供了实现这个功能的API,并且会自动将用户设置以键值对的形式存入S...

weixin_29521249的博客 1597
上一篇: Android OpenGLES 学习笔记
下一篇: Java 备忘
mingming_killer
博客等级 码龄19年 18粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值