添加一級選項以及對應的二級界面:
例如添加聲音選項
1 在 packages/apps/Settings/res/xml/dashboard_categories.xml 添加如下代碼
<dashboard-tile -----添加一級選項的格式
android:id="@+id/sound_settings" ------標題的ID表示符
android:title="@string/sound_settings" ------聲音選項的標題名
android:fragment="com.android.settings.SoundSettings" ------點擊聲音後回調的類,加載二級界面
android:icon="@drawable/ic_settings_display" -----圖標
/>
還有一種寫法
<dashboard-tile
android:id="@+id/navigation_bar_settings"
android:icon="@drawable/ic_settings_navigation_bar"
android:title="@string/navigation_bar_title" >
<intent
android:action="android.intent.action.MAIN"
android:targetClass="com.sprd.settings.navigation.NavigationBarSettings"
android:targetPackage="com.android.settings" />
</dashboard-tile>
一級界面的分類標題(例如設備)
<dashboard-category
android:id="@+id/device_section"
android:key="@string/category_key_device"
android:title="@string/header_category_device" >
2
創建SoundSettings java文件實現activity
路徑 packages/apps/Settings/src/com/android/settings/SoundSettings.java
一開始不會寫,簡單的cp一下同類的java文件即可。
可以看到是extends SettingsPreferenceFragment的類
其中
onCreate(Bundle icicle){ 加載當前要顯示的三級佈局和獲取UI控件實例 } 二級也是
<pre name="code" class="html">addPreferencesFromResource(R.xml.sound_settings);
onPreferenceTreeClick(Preference preference){點擊三級佈局某個選項的回調函數}
其它的需要研究
3 添加布局文件 sound_settings.xml
路徑packages/apps/Settings/res/xml/sound_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
android:title="@string/date_and_time" ----二級界面的標題名稱
settings:keywords="@string/keywords_date_and_time">
<SeekBarDialogPreferenceSnmu ----自定義的進度條對話框 會調用<style type="text/css">P { margin-bottom: 0.08in; }A:link { }</style><span style="font-size:12px;"><span style="font-family:WenQuanYi Zen Hei;"></span></span>SeekBarDialogPreferenceSnmu<span style="font-size:12px;"><span style="font-family:WenQuanYi Zen Hei;"> </span></span>類
android:key="seekbar_time_snmu_1" ----標識
android:title="@string/sound_settings" ----對話框題目
/>
<PreferenceCategory ----二級界面添加小題目格式
android:key="interruption"
android:title="@string/prompt_section_header" > ----小題目名稱
<!--PreferenceScreen
android:persistent="false"
android:ringtoneType="notification" />
<!-- Other sounds -->
<Preference ----二級選項的格式
android:key="other_sounds" ----key
android:title="@string/other_sound_settings" ------標題
android:persistent="false"
android:fragment="com.android.settings.notification.OtherSoundSettings" /> ----點擊回調的fragment 三級界面
<CheckBoxPreference
android:key="checkbox_1"
android:title="@string/usb_mtp_title"
/>
<CheckBoxPreference
android:key="checkbox_2"
android:title="@string/usb_mtp_title"
/>
<CheckBoxPreference
android:key="checkbox_3"
android:title="@string/usb_mtp_title"
/>
</PreferenceCategory>
<PreferenceScreen ---- 二級選項的格式
android:key="brightness"
android:title="@string/brightness"
settings:keywords="@string/keywords_display_brightness_level">
<intent android:action="android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
</PreferenceScreen>
<SwitchPreference android:key="auto_time_snmu"
android:title="@string/date_time_auto"
android:summaryOn="@string/date_time_auto_summaryOn" ---二級選項下面的補充信息
android:summaryOff="@string/date_time_auto_summaryOff"
/>
<VolumePreference
android:key="seekbar_time_snmu"
android:title="@string/date_and_time"
android:icon="@drawable/assistant_top" ---二級選項圖標
/>
<SeekBarPreference
android:key="seekbar_time_snmu"
android:title="@string/date_and_time"
android:icon="@drawable/assistant_top"
/>
<SeekBarDialogPreference
android:key="seekbar_time_snmu"
android:title="@string/date_and_time"
android:icon="@drawable/assistant_top"
android:summary="@string/doze_summary" -------<style type="text/css">P { margin-bottom: 0.08in; }A:link { }java文件中setSummary( 更改</style> java<span style="font-family:WenQuanYi Zen Hei;"><span style="font-size:12px;"><span lang="zh-CN">文件中</span></span></span>setSummary() <span style="font-family:WenQuanYi Zen Hei;"><span style="font-size:12px;"><span lang="zh-CN">更改</span></span></span>
/>
<PreferenceCategory
android:key="sound"
android:title="@string/date_time_set_date" >
<com.android.settings.notification.VolumeSeekBarPreference
android:key="date"
android:title="@string/date_time_set_date"
android:icon="@drawable/assistant_top"
/>
<com.android.settings.notification.VolumeSeekBarPreference
android:key="date_1"
android:title="@string/date_time_set_date"
android:icon="@drawable/assistant_top"
/>
<com.android.settings.notification.VolumeSeekBarPreference
android:key="date_2"
android:title="@string/date_time_set_date"
android:icon="@drawable/assistant_top"
/>
</PreferenceCategory>
</PreferenceScreen>
開關類型:
TwoStatePreference是一個抽象類,它下面的兩個子類CheckBoxPreference和SwitchPreference。
彈出框類型:
彈出框類型的都繼承子虛擬類DialogPreference,分別是EditPreference,MultiCheckPreference,ListPreference,MultiSelectListPreference,SeekBarDialogPreference,VolumePreferenc。
特殊:
RingtonePreference和SeekBarPreference分別是鈴聲選擇和滑塊。
組類型:
組類型都繼承子抽象類PreferenceGroup,其中PreferenceCategory表示分類,PreferenceScreen主要用來表示一個設置界面中的根節點。
什麼是Preference?
Preference翻譯過來是“偏愛,愛好”的意思,在Android中Preference是一個具體的類,它代表着一個最基本的UI塊(可以理解爲ListView中的一個item),並且顯示在Activity或Fragment內置的ListView上面,如圖1-1中的“聲音”選項,就是一個Preference的UI塊表現(這裏爲什麼要說表現呢?因爲Preference本身並不是繼承View的視圖類,更不是activity。它只是代表着一個偏好選項然後通過提供UI塊來展示這些偏好選項)。並且每一個UI塊都會和一個SharePreferences關聯來保存或恢復偏好設置。
4 AndroidManifest.xml 添加activity
路徑 packages/apps/Settings/AndroidManifest.xml
<activity android:name="Settings$SoundSettingsActivity"
android:label="@string/date_and_time"
android:exported="true"
android:taskAffinity="">
5
Settings.java 添加如下:
public static classSoundSettingsActivity extends SettingsActivity { /* empty */ }
路徑packages/apps/Settings/src/com/android/settings/Settings.java
接下來講解下二級選項的特殊控件Preference的使用
例如
SeekBarDialogPreferenceSnmu 添加自定義的preference
這些preference都在framework層中定義,最簡單的方法參考SeekBarDialogPreference 的實現過程,拷貝並修改即可。
添加自定義對話框裏面的佈局
res/layout/preference_dialog_seekbar_snmu.xml (字符串必須@string)
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/RL1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorSecondary"
android:textSize="15sp"
android:text="@string/sound_adjust"
android:singleLine="true" />
<CheckBox
android:id="@+id/CB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_alignParentRight="true"
android:text="@string/sound_without" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorSecondary"
android:textSize="15sp"
android:layout_toLeftOf="@+id/CB"
android:text="@string/sound_adjust"
android:singleLine="true" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/RL2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorSecondary"
android:textSize="15sp"
android:text="@string/sound_adjust"
android:layout_alignLeft="@+id/seekbar1"
android:singleLine="true" />
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:src="@drawable/arrow_up_float"
android:layout_gravity="left"
/>
<SeekBar
android:id="@+id/seekbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_toRightOf="@+id/icon"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/RL3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorSecondary"
android:textSize="15sp"
android:text="@string/sound_adjust"
android:layout_alignLeft="@+id/seekbar2"
android:singleLine="true" />
<ImageView
android:id="@+id/icon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:src="@drawable/arrow_up_float"
android:layout_gravity="left"
/>
<SeekBar
android:id="@+id/seekbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_toRightOf="@+id/icon1"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/RL4"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorSecondary"
android:textSize="15sp"
android:text="@string/sound_adjust"
android:layout_alignLeft="@+id/seekbar3"
android:singleLine="true" />
<ImageView
android:id="@+id/icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:src="@drawable/arrow_up_float"
android:layout_gravity="left"
/>
<SeekBar
android:id="@+id/seekbar3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_toRightOf="@+id/icon2"
/>
</RelativeLayout>
<ImageButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:src ="@drawable/ic_find_previous_holo_light">
</ImageButton>
</LinearLayout>
需要改動的文件有
base/core/res/res/values/attrs.xml :890
./res/res/values/symbols.xml:2332: 聲明添加要用的圖片名字
./res/res/values/styles.xml:1024 風格
./res/res/values/themes.xml:336: 主題
./res/res/values-zh-rCN/strings.xml:1236 增加的字符串
最後編譯
make framework -j4
framework-res.apk
framework.jar