1. 程式人生 > >在選單Setting加入子選單

在選單Setting加入子選單

可以模仿Settings其他選單項加入自己想要的一級或二級子選單項。

比如,在選單Settings里加入一級和二級Usb Mode Settings選單項 (以Android4.4 KK為例)

一. 加入一級子選單項Usb Mode Settings, 加在About phone後面

1. packages/apps/Settings/AndroidManifest.xml
        <activity android:name="Settings$DeviceInfoSettingsActivity"
                android:theme="@style/Theme.Holo.DialogWhenLarge"
                android:label="@string/device_info_settings"
                android:taskAffinity="com.android.settings"
                android:parentActivityName="Settings">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.settings.DEVICE_INFO_SETTINGS" />
                <category android:name="android.intent.category.DEFAULT" />db
                <category android:name="android.intent.category.VOICE_LAUNCH" />
            </intent-filter>
            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                android:value="com.android.settings.DeviceInfoSettings" />
            <meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"
                android:resource="@id/about_settings" />
        </activity>

        <activity android:name="Settings$UsbModeSettingsActivity"
                android:theme="@style/Theme.SubSettingsDialogWhenLarge"
                android:label="@string/usb_mode_settings"
                android:taskAffinity="com.android.settings"
                android:parentActivityName="Settings">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.settings.USB_MODE_SETTINGS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                android:value="com.android.settings.UsbModeSettings" />
            <meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"
                android:resource="@id/usb_mode_settings" />
        </activity>


2. packages/apps/Settings/res/xml/settings_headers.xml 
        <!-- About Device -->
        <dashboard-tile
                android:id="@+id/about_settings"
                android:title="@string/about_settings"
                android:fragment="com.android.settings.DeviceInfoSettings"
                android:icon="@drawable/ic_settings_about"
                />
               
        <!-- Usb Mode -->
        <dashboard-tile
                android:id="@+id/usb_mode_settings"
                android:title="@string/usb_mode_settings"
                android:fragment="com.android.settings.UsbModeSettings"
                android:icon="@drawable/ic_settings_about"
                />


 Lollipop
packages\apps\Settings\res\xml\dashboard_categories.xml       
        <!-- Usb Mode -->
        <dashboard-tile
                android:id="@+id/usb_mode_settings"
                android:title="@string/usb_mode_settings"
                android:fragment="com.android.settings.UsbModeSettings"
                android:icon="@drawable/ic_settings_about"
                />
3. packages\apps\Settings\src\com\android\settings\Settings.java
     public static class DeviceInfoSettingsActivity extends SettingsActivity { /* empty */ }
+    public static class UsbModeSettingsActivity extends SettingsActivity { /* empty */ }
     public static class ApplicationSettingsActivity extends SettingsActivity { /* empty */ }

4. packages/apps/Settings/src/com/android/settings/Settings.java
            DeviceInfoSettings.class.getName(),
+            UsbModeSettings.class.getName(),
             ManageApplications.class.getName(),

Lollipop
packages\apps\Settings\src\com\android\settings\SettingsActivity.java
            DeviceInfoSettings.class.getName(),
+            UsbModeSettings.class.getName(),
            ManageApplications.class.getName(),

5. packages/apps/Settings/res/values/strings.xml
增加新字串

新增部分
6. packages/apps/Settings/res/drawable/
新增圖示

7. 模仿packages/apps/Settings/res/xml/device_info_settings.xml新加一layout檔案
packages/apps/Settings/res/xml/usb_mode_settings.xml
      android:title="@string/about_settings">       
        <!-- Device status - launches activity -->
        <PreferenceScreen android:key="status_info"
                android:title="@string/device_status"
                android:summary="@string/device_status_summary">
            <intent android:action="android.intent.action.MAIN"
                    android:targetPackage="com.android.settings"
                    android:targetClass="com.android.settings.deviceinfo.Status" />
        </PreferenceScreen>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/usb_mode_settings">
    <PreferenceCategory
            android:title="@string/usb_connection_category" />
    <CheckBoxPreference
        android:key="usb_mtp"
        android:title="@string/usb_mtp_title"
        android:summary="@string/usb_mtp_summary"
        />
    <CheckBoxPreference
        android:key="usb_mass_storage"
        android:title="@string/usb_ptp_title"
        android:summary="@string/usb_ptp_summary"
        />
</PreferenceScreen>

8. 模仿packages/apps/Settings/src/com/android/settings/DeviceInfoSettings.java新加一程式碼實現
packages/apps/Settings/src/com/android/settings/UsbModeSettings.java

二. 加入一級子選單項Usb Mode Settings, 加在Developer options下面, 在"Desktop backup password"後面

1.packages/apps/Settings/AndroidManifest.xml
        <activity android:name="SetFullBackupPassword"
                android:theme="@style/Theme.SubSettingsDialogWhenLarge"
                android:exported="false">
        </activity>

        <activity android:name="UsbModeSettings"
                android:theme="@style/Theme.SubSettingsDialogWhenLarge"
                android:exported="false">
        </activity>


2. packages/apps/Settings/res/xml/development_prefs.xml
    <PreferenceScreen
            android:key="local_backup_password"
            android:title="@string/local_backup_password_title"
            android:summary="@string/local_backup_password_summary_none"
            android:persistent="false" >
        <intent
                android:action="android.settings.privacy.SET_FULL_BACKUP_PASSWORD"
                android:targetPackage="com.android.settings"
                android:targetClass="com.android.settings.SetFullBackupPassword" />
    </PreferenceScreen>

    <PreferenceScreen
            android:key="usb_mode"
            android:title="@string/usb_mode_settings"
            android:summary="@string/usb_mode_settings_summary"
        <intent
                android:action="android.intent.action.MAIN"
                android:targetPackage="com.android.settings"
                android:targetClass="com.android.settings.UsbModeSettings" />
    </PreferenceScreen>

3. /packages/apps/Settings/res/values/strings.xml
+    <string name="usb_mode_settings">USB mode settings</string>
......


4. 新加檔案

      res/layout/usb_mode_settings.xml

     packages/apps/Settings/res/drawable/ 圖示
 

    src/com/android/settings/UsbModeSettings.java