Setting 定制(android-6.0)

  • Post author:
  • Post category:其他


添加一级选项以及对应的二级界面:

例如添加声音选项

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









版权声明:本文为w6980112原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。