Android: Modify the view attribute in preference

Source: Internet
Author: User

The android Development Kit preference provides some of our setting tools, including preferenceactivity, preference, preferencescreen, and checkboxpreference. They can be used to conveniently set attributes of applications.

  

Sometimes, we do not need the monotonous UI provided by the system. We usually modify some attributes, such as the view background and font attributes. There are two options at this time:

1. Define a cutom layout for a single application. Of course, this layout should be consistent with the system layout element (otherwise, how can you change it ?), Then add a custom layout to the Android: Layout file in preference. XML (you can retrieve the file name by yourself.

2. Modify preference-related attributes for the entire android system. All interfaces that use preferenceactivity will change accordingly. Here, you only need to modify the corresponding system layout file (in framework/base/CORE/RES/layout), such as preference_category.xml.

1. Let's talk about it first. It is quite common.

First, define your layout file. You can copy this layoutn directly to the framework and mark it as custom_preference.xml.

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="match_parent" android:layout_height="wrap_content"
3 android:minHeight="?android:attr/listPreferredItemHeight"
4 android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
5
6 <RelativeLayout android:layout_width="wrap_content"
7 android:layout_height="wrap_content" android:layout_marginLeft="15dip"
8 android:layout_marginRight="6dip" android:layout_marginTop="6dip"
9 android:layout_marginBottom="6dip" android:layout_weight="1">
10
11 <TextView android:id="@+android:id/title"
12 android:layout_width="wrap_content" android:layout_height="wrap_content"
13 android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge"
14 android:ellipsize="marquee" android:fadingEdge="horizontal"
15 android:textColor="#475ad7"/>
16
17 <TextView android:id="@+android:id/summary"
18 android:layout_width="wrap_content" android:layout_height="wrap_content"
19 android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title"
20 android:textAppearance="?android:attr/textAppearanceSmall"
21 android:textColor="#5ad747"/>
22
23 </RelativeLayout>
24
25 <!-- Preference should place its actual preference widget here. -->
26 <LinearLayout android:id="@+android:id/widget_frame"
27 android:layout_width="wrap_content" android:layout_height="match_parent"
28 android:gravity="center_vertical" android:orientation="vertical"/>
29
30 </LinearLayout>

Then, you can apply the layout file to some preference. XML files.

1 <?xml version="1.0" encoding="utf-8"?>
2 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
3 <CheckBoxPreference android:key="checkbox_key"
4 android:title="checkbox_title"
5 android:summary="checkbox_summery"
6 android:defaultValue="true"
7 android:layout="@layout/custom_preference_layout"></CheckBoxPreference>
8 </PreferenceScreen>

OK. Now, you can use this preference. xml file as usual. You will find that the font color has changed.

  

2. In the second case, we need to build the Android system's compiling environment.

Directly go to the system layout file (in framework/base/CORE/RES/layout) and modify preference. XML. Here is just for the example above. You can also modify other preference files. The content of the preference. xml file is the content of the custom_preference.xml file in the first case.

After modification, restart the mka system, restart the simulator, and OK. The first method has the same effect.

Related Article

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.