以前沒怎么關(guān)注過PreferenceActivity的一些用法只是簡(jiǎn)單了解下,然后剛好今天遇到了某個(gè)機(jī)型上出現(xiàn)了問題:我明明設(shè)置的是黑色的字體封救,在這個(gè)機(jī)器上卻是白色的,然后各種摸索得出了個(gè)推論捣作,頁(yè)面的字體顏色什么的誉结,可能是更隨系統(tǒng)來(lái)的,受系統(tǒng)調(diào)控:
我是這么寫的券躁,
<CheckBoxPreference
android:defaultValue="true"
android:key="weather_use_metric"
android:textColor="@color/black"
android:title="@string/weather_use_metric" />
問題出現(xiàn) 了那沒辦法惩坑,只能想辦法改了,好在現(xiàn)在學(xué)會(huì)了面向google編程也拜,然后搜的一下以舒,找到了個(gè)解決方案,先上怎么解決慢哈,然后再說(shuō)為什么這么干蔓钟。首先建個(gè)layout ,名字隨便嚷鸭(我取名custom_preferece_layout.xml)滥沫,內(nèi)容如下:
<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"
android:textColor="@color/black" />
<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: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>
在這個(gè)布局中修改title,sunmmary的顏色就可以了键俱。
然后再CheckBoxPreference 中加一句:
<CheckBoxPreference
android:defaultValue="true"
android:key="weather_use_metric"
android:layout="@layout/custom_preferece_layout"
android:textColor="@color/black"
/>
然后兰绣,就解決了這個(gè)問題,那為什么這么搞呢编振?進(jìn)入CheckBoxPreference 頂層父類Preference中看缀辩,看到構(gòu)造中的注釋了:
這玩意默認(rèn)的控制了樣式,然后再往下看到一個(gè)方法:
他說(shuō)大部分情況足夠用了,但是這不是出來(lái)問題么雌澄,于是到android的values中的attr下找斋泄,
嗯很貼心,行數(shù)都截出來(lái)了镐牺。頂層默認(rèn)實(shí)現(xiàn)了個(gè)布局炫掐,在CheckBoxPreference的onBindView方法中出現(xiàn)了個(gè)id和一個(gè)方法:
id點(diǎn)進(jìn)去能找到個(gè)布局,然后再父類TwoStatePreference中可以看到這個(gè)方法里面寫了什么睬涧,注意看上面的布局控件的id募胃,你沒看錯(cuò),就是覆蓋了preference_material.xml 中的布局: