电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android 修改Preferences默認樣式的步驟

瀏覽:67日期:2022-09-19 10:24:37

Android開發中難免會遇到參數配置的功能,此時可以通過普通的布局實現,不過android sdk中也為我們提供了Preferences,可以通過配置xml方式實現配置界面的效果。比如手機系統的設置應用就是使用的Preferences:

Android 修改Preferences默認樣式的步驟

如何使用Preferences這里就不說了,你可以新建Activity選擇Settings Activity模板了解它的基本使用,模板默認的界面如下:

Android 修改Preferences默認樣式的步驟

可以看到,非常丑,這里就以修改icon和文字的間距為目標探究如何修改Preferences樣式。

1,查找源碼

以SwitchPreferenceCompat為例,查看其源代碼

首先查看其構造方法:

public class SwitchPreferenceCompat extends TwoStatePreference { /** * Construct a new SwitchPreference with the given style options. * * @param context The {@link Context} that will style this preference * @param attrsStyle attributes that differ from the default * @param defStyleAttr An attribute in the current theme that contains a reference to a style * resource that supplies default values for the view. Can be 0 to not * look for defaults. * @param defStyleRes A resource identifier of a style resource that supplies default values * for the view, used only if defStyleAttr is 0 or can not be found in the * theme. Can be 0 to not look for defaults. */ public SwitchPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);... } public SwitchPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr) {this(context, attrs, defStyleAttr, 0); } public SwitchPreferenceCompat(Context context, AttributeSet attrs) {// 使用R.attr.switchPreferenceCompatStyle作為默認主題樣式this(context, attrs, R.attr.switchPreferenceCompatStyle); } public SwitchPreferenceCompat(Context context) {this(context, null); } ...}

SwitchPreferenceCompat重寫了四個構造方法,其中在兩參數的構造方法中傳入了默認的主題樣式R.attr.switchPreferenceCompatStyle, 這就是一個自定義的屬性,定義在values-values.xml中。那么這個屬性是在哪里賦值的呢,查找一下,它在switchPreferenceCompatStyle中賦了值:

<style name='PreferenceThemeOverlay'>...<item name='switchPreferenceCompatStyle'>@style/Preference.SwitchPreferenceCompat.Material</item>... </style>

繼續查看Preference.SwitchPreferenceCompat.Material

<style name='Preference.SwitchPreferenceCompat.Material'><item name='android:layout'>@layout/preference_material</item><item name='allowDividerAbove'>false</item><item name='allowDividerBelow'>true</item><item name='iconSpaceReserved'>@bool/config_materialPreferenceIconSpaceReserved</item> </style>

此處設置了android:layout屬性,查看該layout:

<!-- preference_material.xml --><?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' android:minHeight='?android:attr/listPreferredItemHeightSmall' android:gravity='center_vertical' android:paddingLeft='?android:attr/listPreferredItemPaddingLeft' android:paddingStart='?android:attr/listPreferredItemPaddingStart' android:paddingRight='?android:attr/listPreferredItemPaddingRight' android:paddingEnd='?android:attr/listPreferredItemPaddingEnd' android:background='?android:attr/selectableItemBackground' android:clipToPadding='false' android:baselineAligned='false'> <include layout='@layout/image_frame'/> <RelativeLayoutandroid:layout_width='0dp'android:layout_height='wrap_content'android:layout_weight='1'android:paddingTop='16dp'android:paddingBottom='16dp'><TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:singleLine='true' android:textAppearance='?android:attr/textAppearanceListItem' android:ellipsize='marquee'/><TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_below='@android:id/title' android:layout_alignLeft='@android:id/title' android:layout_alignStart='@android:id/title' android:layout_gravity='start' android:textAlignment='viewStart' android:textColor='?android:attr/textColorSecondary' android:maxLines='10' /> </RelativeLayout> <!-- Preference should place its actual preference widget here. --> <LinearLayoutandroid: android:layout_width='wrap_content'android:layout_height='match_parent'android:gravity='end|center_vertical'android:paddingLeft='16dp'android:paddingStart='16dp'android:paddingRight='0dp'android:paddingEnd='0dp'android:orientation='vertical'/></LinearLayout>

image_frame.xml

<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:minWidth='56dp' android:gravity='start|center_vertical' android:orientation='horizontal' android:paddingLeft='0dp' android:paddingStart='0dp' android:paddingRight='8dp' android:paddingEnd='8dp' android:paddingTop='4dp' android:paddingBottom='4dp'> <androidx.preference.internal.PreferenceImageViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'app:maxWidth='48dp'app:maxHeight='48dp'/></LinearLayout>

看到這里不禁:臣卜木曹!這不就是每個item的layout嗎?布局是找到了,那怎么修改呢?

2,覆蓋源碼

源碼雖然不能修改,但是可以覆蓋。

于是復制一份preference_material.xml和image_frame.xml,到你的layout目錄下,然后修改image_frame.xml中的minWidth屬性為40dp,在運行一下:

Android 修改Preferences默認樣式的步驟

可以看到,生效了。

分析一下preference_material.xml可知每個item主要有三部分,如下:

Android 修改Preferences默認樣式的步驟

第一部分為圖標區域,第二部分是title和summary區域,第三部分是其他控件區域。

了解了其大體結構,就可以根據需求進行修改了。

注意:第三部分控件是動態添加的,修改時還需要查找一下其具體實現。

比如要修改上圖中的switch button,就要先找到它的布局,查找源碼可知它使用的是preference_widget_switch_compat.xml或preference_widget_switch.xml,之所以有兩個是為了做兼容。那么接下來只需要覆蓋這兩個xml文件,并修改switch樣式即可。效果如下:

Android 修改Preferences默認樣式的步驟

到這里自定義Preferences樣式已經完了,下面介紹的是通用的效果,不僅可以用在Preferences, 也可以用于其他使用水波漣漪效果的場景。

3,點擊水波效果

Preferences默認每個item是有點擊的水波特效的,它是通過android:background='?android:attr/selectableItemBackground'屬性實現。

按說已經很炫酷了,但是遇到事兒多的產品經理非要你改個水波顏色怎么搞?

可以通過自定義Ripple實現,不過還有更簡單的方式,就是重寫android:colorControlHighlight屬性,如下:

<item name='android:colorControlHighlight'>@color/color_item_high_light</item>

效果如下:

Android 修改Preferences默認樣式的步驟

此時產品經理又來了 '你這個實現起來貌似很簡單,那你給點擊時的高亮區域加個邊距并設置成圓角吧'。

一萬頭羊駝奔騰而過!!!

此時就避免不了自定義Ripple了。

新建ripple標簽的drawable,并設置顏色即可實現自定義Ripple:

<?xml version='1.0' encoding='utf-8'?><ripple xmlns:android='http://schemas.android.com/apk/res/android' android:color='@color/teal_700'> </ripple>

然后通過item設置邊距以及圓角,完成代碼:

<?xml version='1.0' encoding='utf-8'?><ripple xmlns:android='http://schemas.android.com/apk/res/android' android:color='@color/teal_700'> <itemandroid:left='8dp'android:right='8dp'><shape android:shape='rectangle'> <solid android:color='?android:attr/colorBackground' /> <corners android:radius='6dp' /></shape> </item></ripple>

運行效果如下:

Android 修改Preferences默認樣式的步驟

以上就是Android 修改Preferences默認樣式的步驟的詳細內容,更多關于Android 修改Preferences默認樣式的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
主站蜘蛛池模板: 上海皓越真空设备有限公司官网-真空炉-真空热压烧结炉-sps放电等离子烧结炉 | 贝壳粉涂料-内墙腻子-外墙腻子-山东巨野七彩贝壳漆业中心 | 硅胶布|电磁炉垫片|特氟龙胶带-江苏浩天复合材料有限公司 | app开发|app开发公司|小程序开发|物联网开发||北京网站制作|--前潮网络 | 手持气象站_便携式气象站_农业气象站_负氧离子监测站-山东万象环境 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 | 泡沫消防车_水罐消防车_湖北江南专用特种汽车有限公司 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 通风气楼_通风天窗_屋顶风机-山东美创通风设备有限公司 | 多功能真空滤油机_润滑油全自动滤油机_高效真空滤油机价格-重庆润华通驰 | 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 硬度计_影像测量仪_维氏硬度计_佛山市精测计量仪器设备有限公司厂家 | 上海道勤塑化有限公司| 雷达液位计_超声波风速风向仪_雨量传感器_辐射传感器-山东风途物联网 | 煤棒机_增碳剂颗粒机_活性炭颗粒机_木炭粉成型机-巩义市老城振华机械厂 | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 上海租车公司_上海包车_奔驰租赁_上海商务租车_上海谐焕租车 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 合肥礼品公司-合肥礼品定制-商务礼品定制公司-安徽柏榽商贸有限公司 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 数显恒温油浴-电砂浴-高温油浴振荡器-常州迈科诺仪器有限公司 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | 岩石钻裂机-液压凿岩机-劈裂机-挖改钻_湖南烈岩科技有限公司 | 大巴租车平台承接包车,通勤班车,巴士租赁业务 - 鸿鸣巴士 | 电线电缆厂家|沈阳电缆厂|电线厂|沈阳英联塑力线缆有限公司 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 污水提升器,污水提升泵,污水提升装置-德国泽德(zehnder)水泵系统有限公司 | 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 澳威全屋定制官网|极简衣柜十大品牌|衣柜加盟代理|全屋定制招商 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 天津云仓-天津仓储物流-天津云仓一件代发-顺东云仓 | 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 导电银胶_LED封装导电银胶_半导体封装导电胶厂家-上海腾烁 | 三板富 | 专注于新三板的第一垂直服务平台| 山东柳店新能源科技有限公司| EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 | 集装箱箱号识别_自重载重图像识别_铁路车号自动识别_OCR图像识别 | 滁州高低温冲击试验箱厂家_安徽高低温试验箱价格|安徽希尔伯特 | 雷冲击高压发生器-水内冷直流高压发生器-串联谐振分压器-武汉特高压电力科技有限公司 |