1.android settings源码的source code路径为:
kikat_4.4_CTS\packages\apps\Settings
2.settings主界面UI布局
Settings的启动类为:
/** * Top-level settings activity to handle single pane and double pane UI layout. */ public class Settings extends PreferenceActivity implements ButtonBarHandler, OnAccountsUpdateListener
从继承关系可以看出,Settings类继承于PreferenceActivity,因此具有Preference的一些属性和结构。
从Google settings的UI看出,左边是一些tab,右边是tab对应的内容,那么这些tab以及tab对应的内容主要是什么,以及如何显示的呢?
对于tab的显示,对应的code为:
/** * Populate the activity with the top-level headers. */ @Override public void onBuildHeaders(List<Header> headers) { if (!onIsHidingHeaders()) { loadHeadersFromResource(R.xml.settings_headers, headers); updateHeaderList(headers); } }
可以看出,左边的tab实际是一些Header,从settings_headers.xml中获取这些header。settings_header.xml对应的code为:
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2010 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 at 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. --> <preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> <!-- WIRELESS and NETWORKS --> <header android:id="@+id/wireless_section" android:title="@string/header_category_wireless_networks" /> <!-- Wifi --> <!-- MStar Android Patch Begin --> <header android:id="@+id/wifi_settings" android:fragment="com.android.settings.wifi.TvWifiSettings" android:title="@string/wifi_settings_title" android:icon="@drawable/ic_settings_wireless" /> <!-- MStar Android Patch End --> <!-- Ethernet --> <header android:id="@+id/ethernet_settings" android:fragment="com.android.settings.ethernet.EthernetSettings" android:title="@string/ethernet_status_title" android:icon="@drawable/ic_settings_ethernet" /> <!-- Bluetooth --> <header android:id="@+id/bluetooth_settings" android:fragment="com.android.settings.bluetooth.TvBluetoothSettings" android:title="@string/bluetooth_settings_title" android:icon="@drawable/ic_settings_bluetooth2" /> <!-- Data Usage --> <header android:id="@+id/data_usage_settings" android:fragment="com.android.settings.DataUsageSummary" android:title="@string/data_usage_summary_title" android:icon="@drawable/ic_settings_data_usage" /> <!-- Operator hook --> <header android:fragment="com.android.settings.WirelessSettings" android:id="@+id/operator_settings"> <intent android:action="com.android.settings.OPERATOR_APPLICATION_SETTING" /> </header> <!-- Other wireless and network controls --> <header android:id="@+id/wireless_settings" android:title="@string/radio_controls_title" android:breadCrumbTitle="@string/wireless_networks_settings_title" android:fragment="com.android.settings.WirelessSettings" android:icon="@drawable/empty_icon" /> <!-- DEVICE --> <header android:id="@+id/device_section" android:title="@string/header_category_device" /> <!-- Home --> <header android:id="@+id/home_settings" android:icon="@drawable/ic_settings_home" android:fragment="com.android.settings.HomeSettings" android:title="@string/home_settings" /> <!-- Sound --> <header android:id="@+id/sound_settings" android:icon="@drawable/ic_settings_sound" android:fragment="com.android.settings.SoundSettings" android:title="@string/sound_settings" /> <!-- Display --> <header android:id="@+id/display_settings" android:icon="@drawable/ic_settings_display" android:fragment="com.android.settings.DisplaySettings" android:title="@string/display_settings" /> <!-- Storage --> <header android:id="@+id/storage_settings" android:fragment="com.android.settings.deviceinfo.Memory" android:icon="@drawable/ic_settings_storage" android:title="@string/storage_settings" /> <!-- Battery --> <header android:id="@+id/battery_settings" android:fragment="com.android.settings.fuelgauge.PowerUsageSummary" android:icon="@drawable/ic_settings_battery" android:title="@string/power_usage_summary_title" /> <!-- Application Settings --> <header android:fragment="com.android.settings.applications.ManageApplications" android:icon="@drawable/ic_settings_applications" android:title="@string/applications_settings" android:id="@+id/application_settings" /> <!-- Manage users --> <header android:fragment="com.android.settings.users.UserSettings" android:icon="@drawable/ic_settings_multiuser" android:title="@string/user_settings_title" android:id="@+id/user_settings" /> <!-- Manage NFC payment apps --> <header android:fragment="com.android.settings.nfc.PaymentSettings" android:icon="@drawable/ic_settings_nfc_payment" android:title="@string/nfc_payment_settings_title" android:id="@+id/nfc_payment_settings" /> <!-- Manufacturer hook --> <header android:fragment="com.android.settings.WirelessSettings" android:id="@+id/manufacturer_settings"> <intent android:action="com.android.settings.MANUFACTURER_APPLICATION_SETTING" /> </header> <!-- PERSONAL --> <header android:id="@+id/personal_section" android:title="@string/header_category_personal" /> <!-- Location --> <header android:fragment="com.android.settings.location.LocationSettings" android:icon="@drawable/ic_settings_location" android:title="@string/location_settings_title" android:id="@+id/location_settings" /> <!-- Security --> <header android:fragment="com.android.settings.SecuritySettings" android:icon="@drawable/ic_settings_security" android:title="@string/security_settings_title" android:id="@+id/security_settings" /> <!-- Language --> <header android:id="@+id/language_settings" android:fragment="com.android.settings.inputmethod.InputMethodAndLanguageSettings" android:icon="@drawable/ic_settings_language" android:title="@string/language_settings" /> <!-- Backup and reset --> <header android:fragment="com.android.settings.PrivacySettings" android:icon="@drawable/ic_settings_backup" android:title="@string/privacy_settings" android:id="@+id/privacy_settings" /> <!-- @Patch: GTV specific changes --> <!-- MStar Android Patch Begin --> <!-- Update --> <header android:id="@+id/update_settings" android:fragment="com.android.settings.update.UpdateSettings" android:title="@string/update_settings" /> <!-- MStar Android Patch End --> <!-- ACCOUNTS section --> <header android:id="@+id/account_settings" android:title="@string/account_settings" /> <header android:id="@+id/account_add" android:title="@string/add_account_label" android:icon="@drawable/ic_menu_add_dark"> <intent android:action="android.settings.ADD_ACCOUNT_SETTINGS"/> </header> <!-- SYSTEM --> <header android:id="@+id/system_section" android:title="@string/header_category_system" /> <!-- Date & Time --> <header android:id="@+id/date_time_settings" android:fragment="com.android.settings.DateTimeSettings" android:icon="@drawable/ic_settings_date_time" android:title="@string/date_and_time_settings_title" /> <!-- Accessibility feedback --> <header android:id="@+id/accessibility_settings" android:fragment="com.android.settings.accessibility.AccessibilitySettings" android:icon="@drawable/ic_settings_accessibility" android:title="@string/accessibility_settings" /> <!-- Print --> <header android:id="@+id/print_settings" android:fragment="com.android.settings.print.PrintSettingsFragment" android:icon="@*android:drawable/ic_print" android:title="@string/print_settings" /> <!-- Development --> <header android:id="@+id/development_settings" android:fragment="com.android.settings.DevelopmentSettings" android:icon="@drawable/ic_settings_development" android:title="@string/development_settings_title" /> <!-- About Device --> <header android:id="@+id/about_settings" android:fragment="com.android.settings.DeviceInfoSettings" android:icon="@drawable/ic_settings_about" android:title="@string/about_settings" /> </preference-headers>
从上述xml可以看出,header的内容是通过fragment来显示,定义了每一个header对应的fragment。
在private void updateHeaderList(List<Header> target)函数中,会根据当前平台是否支持某项feature,决定是否显示对应的header:
else if (id == R.id.wifi_settings) { // Remove WiFi Settings if WiFi service is not available. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) { target.remove(i); } } else if (id == R.id.bluetooth_settings) { // Remove Bluetooth Settings if Bluetooth service is not available. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) { target.remove(i); } }