When preferenceactivity is used, the layout file format is generally as follows:
[HTML]
View plaincopyprint?
- <Preferencecategory
- Android: Title = "@ string/launch_preferences">
- <! -- This preferencescreen tag sends the user to a new fragment
- Preferences. If running in a large screen, they can be embedded
- Inside of the overall preferences UI. -->
- <Preferencescreen
- Android: fragment = "com. example. Android. APIs. Preference. preferencewithheaders $ prefs1fragmentinner"
- Android: Title = "@ string/title_fragment_preference"
- Android: Summary = "@ string/summary_fragment_preference">
- <! -- Arbitrary key/value pairs can be encoded for fragment arguments -->
- <Extra Android: Name = "somekey" Android: value = "someprefvalue"/>
- </Preferencescreen>
- <! -- This preferencescreen tag sends the user to a completely different
- Activity, switching out of the current preferences UI. -->
- <Preferencescreen
- Android: Title = "@ string/title_intent_preference"
- Android: Summary = "@ string/summary_intent_preference">
- <Intent Android: Action = "android. Intent. Action. View"
- Android: Data = "http://www.android.com"/>
- </Preferencescreen>
- </Preferencecategory>
<PreferenceCategory android:title="@string/launch_preferences"> <!-- This PreferenceScreen tag sends the user to a new fragment of preferences. If running in a large screen, they can be embedded inside of the overall preferences UI. --> <PreferenceScreen android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner" android:title="@string/title_fragment_preference" android:summary="@string/summary_fragment_preference"> <!-- Arbitrary key/value pairs can be included for fragment arguments --> <extra android:name="someKey" android:value="somePrefValue" /> </PreferenceScreen> <!-- This PreferenceScreen tag sends the user to a completely different activity, switching out of the current preferences UI. --> <PreferenceScreen android:title="@string/title_intent_preference" android:summary="@string/summary_intent_preference"> <intent android:action="android.intent.action.VIEW" android:data="http://www.android.com" /> </PreferenceScreen> </PreferenceCategory>
However, we cannot control the title and summary font styles. The system styles are used.
How can I modify the font and color of title and summary?
Preferencescreen is used as an example:
First, the layout file of preferencescreen is in the Framework.
The location is as follows:/framework/base/CORE/RES/layout/preference. xml:
[HTML]
View plaincopyprint?
- <? 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 before t in compliance with the license.
- You may obtain a copy of the license
- 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.
- Preference is able to place a specific widget for its particle
- 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"/>
- <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 shocould 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>
<?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 athttp://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" /> <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>
The layout file of preferencescreen displays the title and summary of the two textviews respectively.
In each app, We can redefine the preferencescreen style according to this style. For example, we can define a custom_preference.xml file with the content similar to the above, but re-define the text size and color, in
[HTML]
View plaincopyprint?
- <Preferencescreen
- Android: fragment = "com. example. Android. APIs. Preference. preferencewithheaders $ prefs1fragmentinner"
- Android: Title = "@ string/title_fragment_preference"
- Android: Summary = "@ string/summary_fragment_preference">
- <! -- Arbitrary key/value pairs can be encoded for fragment arguments -->
- <Extra Android: Name = "somekey" Android: value = "someprefvalue"/>
- </Preferencescreen>
<PreferenceScreen android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner" android:title="@string/title_fragment_preference" android:summary="@string/summary_fragment_preference"> <!-- Arbitrary key/value pairs can be included for fragment arguments --> <extra android:name="someKey" android:value="somePrefValue" /> </PreferenceScreen>
Add an attribute Android: Layout = "@ layout/custom_preference" to load the layout file of your own defined preference.
You can also redefine the corresponding
Prefercencecategory Style File --------------- framework/base/CORE/RES/preference_category.xml
Checkboxpreference Style File --------------- frameworks/base/CORE/RES/layout/preference_widget_checkbox.xml
Edittextpreference Style File --------------- frameworks/base/CORE/RES/layout/preference_dialog_edittext.xml
Of course, there may be other better methods, and we hope to have more exchanges with you.