Android custom preferencescreen

Source: Internet
Author: User

When preferenceactivity is used, the layout file format is generally as follows:

[HTML]
View plaincopyprint?
  1. <Preferencecategory
  2. Android: Title = "@ string/launch_preferences">
  3. <! -- This preferencescreen tag sends the user to a new fragment
  4. Preferences. If running in a large screen, they can be embedded
  5. Inside of the overall preferences UI. -->
  6. <Preferencescreen
  7. Android: fragment = "com. example. Android. APIs. Preference. preferencewithheaders $ prefs1fragmentinner"
  8. Android: Title = "@ string/title_fragment_preference"
  9. Android: Summary = "@ string/summary_fragment_preference">
  10. <! -- Arbitrary key/value pairs can be encoded for fragment arguments -->
  11. <Extra Android: Name = "somekey" Android: value = "someprefvalue"/>
  12. </Preferencescreen>
  13. <! -- This preferencescreen tag sends the user to a completely different
  14. Activity, switching out of the current preferences UI. -->
  15. <Preferencescreen
  16. Android: Title = "@ string/title_intent_preference"
  17. Android: Summary = "@ string/summary_intent_preference">
  18. <Intent Android: Action = "android. Intent. Action. View"
  19. Android: Data = "http://www.android.com"/>
  20. </Preferencescreen>
  21. </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?
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! -- Copyright (c) 2006 the android open source project
  3. Licensed under the Apache license, version 2.0 (the "License ");
  4. You may not use this file before t in compliance with the license.
  5. You may obtain a copy of the license
  6. Http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. Distributed under the license is distributed on an "as is" basis,
  9. Without warranties or conditions of any kind, either express or implied.
  10. See the license for the specific language governing permissions and
  11. Limitations under the license.
  12. -->
  13. <! -- Layout for a preference in a preferenceactivity.
  14. Preference is able to place a specific widget for its particle
  15. Type in the "widget_frame" layout. -->
  16. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  17. Android: layout_width = "match_parent"
  18. Android: layout_height = "wrap_content"
  19. Android: minheight = "? Android: ATTR/listpreferreditemheight"
  20. Android: gravity = "center_vertical"
  21. Android: paddingright = "? Android: ATTR/scrollbarsize ">
  22. <Relativelayout
  23. Android: layout_width = "wrap_content"
  24. Android: layout_height = "wrap_content"
  25. Android: layout_marginleft = "15dip"
  26. Android: layout_marginright = "6dip"
  27. Android: layout_margintop = "6dip"
  28. Android: layout_marginbottom = "6dip"
  29. Android: layout_weight = "1">
  30. <Textview Android: Id = "@ + Android: ID/Title"
  31. Android: layout_width = "wrap_content"
  32. Android: layout_height = "wrap_content"
  33. Android: singleline = "true"
  34. Android: textappearance = "? Android: ATTR/textappearancelarge"
  35. Android: ellipsize = "marquee"
  36. Android: fadingedge = "horizontal"/>
  37. <Textview Android: Id = "@ + Android: ID/Summary"
  38. Android: layout_width = "wrap_content"
  39. Android: layout_height = "wrap_content"
  40. Android: layout_below = "@ Android: ID/Title"
  41. Android: layout_alignleft = "@ Android: ID/Title"
  42. Android: textappearance = "? Android: ATTR/textappearancesmall"
  43. Android: maxlines = "4"/>
  44. </Relativelayout>
  45. <! -- Preference shocould place its actual preference widget here. -->
  46. <Linearlayout Android: Id = "@ + Android: ID/widget_frame"
  47. Android: layout_width = "wrap_content"
  48. Android: layout_height = "match_parent"
  49. Android: gravity = "center_vertical"
  50. Android: Orientation = "vertical"/>
  51. </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?
  1. <Preferencescreen
  2. Android: fragment = "com. example. Android. APIs. Preference. preferencewithheaders $ prefs1fragmentinner"
  3. Android: Title = "@ string/title_fragment_preference"
  4. Android: Summary = "@ string/summary_fragment_preference">
  5. <! -- Arbitrary key/value pairs can be encoded for fragment arguments -->
  6. <Extra Android: Name = "somekey" Android: value = "someprefvalue"/>
  7. </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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.