PreferenceScreen 中如何自定义SwitchPreferenceCompat的布局

Android Preference 使用请看这篇

Android Preference使用

系统设置的代码:

public class SetActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.settings_activity);  if (savedInstanceState == null) {  getSupportFragmentManager()  .beginTransaction()  .replace(R.id.settings, new SettingsFragment())  .commit();  }  ActionBar actionBar = getSupportActionBar();  if (actionBar != null) {  actionBar.setDisplayHomeAsUpEnabled(true);  }
}  public static class SettingsFragment extends PreferenceFragmentCompat {  @Override  public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {  setPreferencesFromResource(R.xml.set, rootKey);  }
}

set.xml 如下:

  <PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto">>  <PreferenceCategory app:title="@string/messages_header">  <EditTextPreference            app:key="signature"  app:title="@string/signature_title"  app:useSimpleSummaryProvider="true" />  <ListPreference            app:defaultValue="reply"  app:entries="@array/reply_entries"  app:entryValues="@array/reply_values"  app:key="reply"  app:title="@string/reply_title"  app:useSimpleSummaryProvider="true" />  </PreferenceCategory>   <PreferenceCategory app:title="@string/sync_header">  <SwitchPreferenceCompat           app:key="sync"  app:title="@string/sync_title" />  <SwitchPreferenceCompat            app:dependency="sync"  app:key="attachment"  app:summaryOff="@string/attachment_summary_off"  app:summaryOn="@string/attachment_summary_on"  app:title="@string/attachment_title" />  </PreferenceCategory></PreferenceScreen>

默认的系统设置页面如下:

不符合要求,如何修改SwitchPreferenceCompat的布局属性和SwitchCompat呢:

设置layout 布局

<SwitchPreferenceCompat  app:key="sync"  app:title="@string/sync_title"app:layout="@layout/layout_setting_item"  />

layout_setting_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:gravity="center_vertical"android:background="@null"android:minHeight="@dimen/dp_40"><!--   style="?android:attr/borderlessButtonStyle"--><ImageViewandroid:id="@android:id/icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="@dimen/dp_20"android:layout_gravity="center" /><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="10dip"android:layout_marginTop="6dip"android:layout_marginEnd="6dip"android:layout_marginBottom="6dip"android:layout_weight="1"><TextViewandroid:id="@android:id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ellipsize="marquee"android:fadingEdge="horizontal"android:singleLine="true"android:textAppearance="?android:attr/textAppearanceLarge"android:textColor="@color/black_222"android:textSize="@dimen/sp_15"/><TextViewandroid:id="@android:id/summary"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@android:id/title"android:layout_alignStart="@android:id/title"android:maxLines="4"android:textAppearance="?android:attr/textAppearanceSmall"android:textColor="?android:attr/textColorSecondary" /></RelativeLayout><androidx.appcompat.widget.SwitchCompatandroid:id="@+id/switchWidget"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@null"android:translationX="@dimen/dp_12"android:checked="true"android:thumb="@drawable/switdth="wrap_content"android:layout_height="wrap_content"/>-->
</LinearLayout>

这样就可以替换原有的布局属性,id 需要声明为@android:id/

看到SwitchCompat 用的@+id/switchWidget ,为啥不用@android:id/switch_widget 或者@+id/switch_widget,

有2点原因:1.@android:id/switch_widget 在android7.0以上才能使用 2.用了以后无法和该item的布局进行点击事件关联

SwitchPreferenceCompat 中的onBindViewHolder方法也标明了id

    @Overridepublic void onBindViewHolder(PreferenceViewHolder holder) {super.onBindViewHolder(holder);View switchView = holder.findViewById(R.id.switchWidget);syncSwitchView(switchView);syncSummaryView(holder);}

修改后的布局如下:

Preference相关联的xml文件如下:preference.xml,preference_material.xml

参考文章:

Android 修改Preferences默认样式的步骤

Preference的 简单讲解

PreferenceScreen 中如何自定义SwitchPreferenceCompat的布局相关推荐

  1. 【翻译】在Ext JS和Sencha Touch中创建自定义布局

    原文:Creating Custom Layouts in Ext JS and Sencha Touch 布局系统是Sencha框架中最强大和最独特的一部分.布局会处理应用程序中每个组件的大小和位置 ...

  2. [MOSS 译]如何:在WEB内容查询部件中使用自定义的字段

    译者的话:数据视图可以很方便地来交叉或是使用单独的数据源来显示各种视图,但是对于开发人员来说,数据视图有一个不太方便的功能就是开发或是定制出的数据视图根本没有办法重用,你不能像使用WEB部件一样随意地 ...

  3. 如何在android中创建自定义对话框?

    本文翻译自:How to create a Custom Dialog box in android? I want to create a custom dialog box like below ...

  4. Android中自定义View的研究 -- 在XML中引用自定义View

    如果在一直使用SetContentView(new HellwView(this)觉得总是少了一点东西,少了什么了,失去了Android中使用XML定义组件的便携性,这种感觉让人很不爽,呵呵,在这节里 ...

  5. Expo大作战(十二)--expo中的自定义样式Custom font,以及expo中的路由RouteNavigation

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  6. 浅谈android中的自定义封装易用的Dialog

    转载地址:http://blog.csdn.net/u013064109/article/details/51990526 好久没写Android的博客,最近在做一个android的项目,里面用到我们 ...

  7. Android XML中引用自定义内部类view的四个why

    今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...

  8. android自定义进度条_Android中的自定义进度栏

    android自定义进度条 Custom progress bar in android application gives it a personal touch. In this tutorial ...

  9. 如何在PowerPoint中创建自定义模板

    PowerPoint provides extremely useful resources called templates that automatically construct the fou ...

  10. NI Multisim元件库:在Multisim中创建自定义元器件

    转载于: http://www.ni.com/tutorial/3173/zhs/ 概览 「在Multisim中创建自定义元器件」与「在 NI Ultiboard中创建自定义元器件」为您提供了关于如何 ...

最新文章

  1. Liferay标签(一):liferay-ui:search-container(1)
  2. CTF-RSA-tool 安装全过程
  3. AC自动机解决字符集很大的情况(可持久化数组优化getfail的过程)
  4. 苹果开始尝试直接从自家零售店发货 更快送达消费者手中
  5. Ajax实现前台传值到后台
  6. Unity3D常用API
  7. Jquery(十)jqueryUI常用功能实战
  8. Dell服务器开启CPU虚拟化
  9. A. Captain Flint and Crew Recruitment
  10. VPS上安装Zpanle面板
  11. java实现报数游戏
  12. 如何关闭电脑的休眠功能
  13. MATLAB用梯度法求解目标函数,机械优化设计作业——梯度法求解
  14. 关于传递函数的双边线性Z变换+差分方程
  15. 前端html通过鼠标操作进行样式的更改
  16. 《自私的基因》理查德道金斯 epub+mobi+azw3
  17. python 百度地图api
  18. 【BATJ面试必会】JAVA面试到底需要掌握什么?【下】
  19. 历史上的今天:网景浏览器诞生;ENIAC 首席设计师出生;全球首例全机器人手术...
  20. 保定 机器人焊接加工厂_保定上下料机器人厂家

热门文章

  1. Arcpy基础入门-1、如何使用arcpy
  2. 3D打印自动支撑算法
  3. 阿里云如何启动mysql数据库_阿里云服务器启动mysql
  4. python分析财务报表
  5. 免费搭建网易云音乐API
  6. Win7系统桌面设置便签与备忘录的方法
  7. 详解Android定位,AndroidGPS定位详解(1)
  8. Android OpenCv实现拍照搜题功能实现步骤
  9. 学小易电脑端——大学生搜题平台
  10. node.js与npm下载及配置流程