Android 修改Preferences默認(rèn)樣式的步驟
Android開發(fā)中難免會(huì)遇到參數(shù)配置的功能,此時(shí)可以通過(guò)普通的布局實(shí)現(xiàn),不過(guò)android sdk中也為我們提供了Preferences,可以通過(guò)配置xml方式實(shí)現(xiàn)配置界面的效果。比如手機(jī)系統(tǒng)的設(shè)置應(yīng)用就是使用的Preferences:

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

可以看到,非常丑,這里就以修改icon和文字的間距為目標(biāo)探究如何修改Preferences樣式。
1,查找源碼
以SwitchPreferenceCompat為例,查看其源代碼
首先查看其構(gòu)造方法:
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 attrs Style 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作為默認(rèn)主題樣式
this(context, attrs, R.attr.switchPreferenceCompatStyle);
}
public SwitchPreferenceCompat(Context context) {
this(context, null);
}
...
}
SwitchPreferenceCompat重寫了四個(gè)構(gòu)造方法,其中在兩參數(shù)的構(gòu)造方法中傳入了默認(rèn)的主題樣式R.attr.switchPreferenceCompatStyle, 這就是一個(gè)自定義的屬性,定義在values-values.xml中。那么這個(gè)屬性是在哪里賦值的呢,查找一下,它在switchPreferenceCompatStyle中賦了值:
<style name="PreferenceThemeOverlay">
...
<item name="switchPreferenceCompatStyle">@style/Preference.SwitchPreferenceCompat.Material</item>
...
</style>
繼續(xù)查看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>
此處設(shè)置了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"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem"
android:ellipsize="marquee"/>
<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:layout_alignStart="@android:id/title"
android:layout_gravity="start"
android:textAlignment="viewStart"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="10"
style="@style/PreferenceSummaryTextStyle"/>
</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="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:id="@+id/icon_frame"
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.PreferenceImageView
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:maxWidth="48dp"
app:maxHeight="48dp"/>
</LinearLayout>
看到這里不禁:臣卜木曹!這不就是每個(gè)item的layout嗎?布局是找到了,那怎么修改呢?
2,覆蓋源碼
源碼雖然不能修改,但是可以覆蓋。
于是復(fù)制一份preference_material.xml和image_frame.xml,到你的layout目錄下,然后修改image_frame.xml中的minWidth屬性為40dp,在運(yùn)行一下:

可以看到,生效了。
分析一下preference_material.xml可知每個(gè)item主要有三部分,如下:

第一部分為圖標(biāo)區(qū)域,第二部分是title和summary區(qū)域,第三部分是其他控件區(qū)域。
了解了其大體結(jié)構(gòu),就可以根據(jù)需求進(jìn)行修改了。
注意:第三部分控件是動(dòng)態(tài)添加的,修改時(shí)還需要查找一下其具體實(shí)現(xiàn)。
比如要修改上圖中的switch button,就要先找到它的布局,查找源碼可知它使用的是preference_widget_switch_compat.xml或preference_widget_switch.xml,之所以有兩個(gè)是為了做兼容。那么接下來(lái)只需要覆蓋這兩個(gè)xml文件,并修改switch樣式即可。效果如下:

到這里自定義Preferences樣式已經(jīng)完了,下面介紹的是通用的效果,不僅可以用在Preferences, 也可以用于其他使用水波漣漪效果的場(chǎng)景。
3,點(diǎn)擊水波效果
Preferences默認(rèn)每個(gè)item是有點(diǎn)擊的水波特效的,它是通過(guò)android:background="?android:attr/selectableItemBackground"屬性實(shí)現(xiàn)。
按說(shuō)已經(jīng)很炫酷了,但是遇到事兒多的產(chǎn)品經(jīng)理非要你改個(gè)水波顏色怎么搞?
可以通過(guò)自定義Ripple實(shí)現(xiàn),不過(guò)還有更簡(jiǎn)單的方式,就是重寫android:colorControlHighlight屬性,如下:
<item name="android:colorControlHighlight">@color/color_item_high_light</item>
效果如下:

此時(shí)產(chǎn)品經(jīng)理又來(lái)了 "你這個(gè)實(shí)現(xiàn)起來(lái)貌似很簡(jiǎn)單,那你給點(diǎn)擊時(shí)的高亮區(qū)域加個(gè)邊距并設(shè)置成圓角吧"。
一萬(wàn)頭羊駝奔騰而過(guò)?。。?/p>
此時(shí)就避免不了自定義Ripple了。
新建ripple標(biāo)簽的drawable,并設(shè)置顏色即可實(shí)現(xiàn)自定義Ripple:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/teal_700">
</ripple>
然后通過(guò)item設(shè)置邊距以及圓角,完成代碼:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/teal_700">
<item
android:left="8dp"
android:right="8dp">
<shape android:shape="rectangle">
<solid android:color="?android:attr/colorBackground" />
<corners android:radius="6dp" />
</shape>
</item>
</ripple>
運(yùn)行效果如下:

以上就是Android 修改Preferences默認(rèn)樣式的步驟的詳細(xì)內(nèi)容,更多關(guān)于Android 修改Preferences默認(rèn)樣式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
- Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能
- Android開發(fā)中4個(gè)常用的工具類【Toast、SharedPreferences、網(wǎng)絡(luò)及屏幕操作】
- Android數(shù)據(jù)共享 sharedPreferences 的使用方法
- Android中SharedPreferences簡(jiǎn)單使用實(shí)例
- Android中使用SharedPreferences完成記住賬號(hào)密碼的功能
- Android SharedPreferences四種操作模式使用詳解
- Android通過(guò)SharedPreferences實(shí)現(xiàn)自動(dòng)登錄記住用戶名和密碼功能
- Android SharedPreferences存儲(chǔ)的正確寫法
- Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)功能
相關(guān)文章
Android Service(不和用戶交互應(yīng)用組件)案例分析
Service是在一段不定的時(shí)間運(yùn)行在后臺(tái),不和用戶交互應(yīng)用組件,本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12
自定義GridView并且實(shí)現(xiàn)拖拽(附源碼)
本文實(shí)現(xiàn)了GridView的拖拽功能,原理很簡(jiǎn)單只是在交換位置上記錄了X軸的相關(guān)坐標(biāo),計(jì)算了X軸的相關(guān)變量,實(shí)例代碼如下,感興趣的額朋友可以參考下哈2013-06-06
Android項(xiàng)目中使用HTTPS配置的步驟詳解
這篇文章主要給大家介紹了關(guān)于Android項(xiàng)目中使用HTTPS配置步驟的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
Android UI控件之ImageSwitcher實(shí)現(xiàn)圖片切換效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件之ImageSwitcher實(shí)現(xiàn)圖片切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android App開發(fā)中自定義View和ViewGroup的實(shí)例教程
這篇文章主要介紹了Android App開發(fā)中自定義View和ViewGroup的實(shí)例教程,分別介紹了進(jìn)度條和圖片上傳并排列的例子,效果很好很強(qiáng)大,需要的朋友可以參考下2016-05-05
基于Android實(shí)現(xiàn)3D翻頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了基于Android實(shí)現(xiàn)3D翻頁(yè)效果的具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
SwipeLayout框架實(shí)現(xiàn)側(cè)拉刪除編輯功能
這篇文章主要為大家詳細(xì)介紹了SwipeLayout框架實(shí)現(xiàn)側(cè)拉刪除編輯功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08

