====================start=============================================================================
Android系统SetttingAPP源码分析:
5.1之前的setting版本:平板布局是,左边为header,右边是关联Prefenrence的Fragment
5.1Setting系统应用源码分析:
入口Settings.java(空实现)
===》父类SettingActivity.java,onCreate()中setContentView(R.layout.settings_main_dashboard)
===》settings_main_dashboard中只有一个帧布局,id为main_content用于Fragment的切换
===》onCreate中继续查看,发现switchToFragment(DashboardSummary.class.getName(), xxx,xx)
===》接着查看DashboardSummary.java,此继承Fragment
===》视图为View rootView = inflater.inflate(R.layout.dashboard, xxx,xx)
可滑动的垂直线性布局,用于存放header
===》rebuildUI(context)
===》DashboardSummary向SettingsActivity拿数据更新UI,((SettingsActivity) context).getDashboardCategories(true);
===》接着看看SettingsActivity怎么拿数据
===》List<DashboardCategory> getDashboardCategories(boolean forceRefresh)
===》buildDashboardCategories(List<DashboardCategory> categories)
===》loadCategoriesFromResource(R.xml.dashboard_categories, categories)
===》找到dashboard_categories.xml
===》查看组标签<dashboard-categories>和成员标签<dashboard-tile>
以显示为例:
<!-- Display -->
<dashboard-tile
android:id="@+id/display_settings"
android:title="@string/display_settings"
android:fragment="com.android.settings.DisplaySettings"
android:icon="@drawable/ic_settings_display"/>
===》可见比较关心的是fragment="com.android.settings.DisplaySettings"属性
===》查看DisplaySettings
===》DisplaySettings继承SettingsPreferenceFragment并实现Preference.OnPreferenceChangeListener,OnPreferenceClickListener,Indexable
===》onCreate中有 addPreferencesFromResource(R.xml.display_settings);
===》找到display_settings.xml,其跟标签<PreferenceScreen> ,子标签有很多类型<XxxPreference>
以自动亮度调节开关为例:
<SwitchPreference
android:key="auto_brightness"
android:title="自动调节亮度"
settings:keywords="变暗 调暗 屏幕 触摸屏 电池"
android:summary="根据环境光线情况优化亮度"
android:persistent="false" />
===》可见关键还是这个key属性
===》查看DisplaySettings.java代码中有private static final String KEY_AUTO_BRIGHTNESS = "auto_brightness";
===》在onCreate中发现此代码:
if (isAutomaticBrightnessAvailable(getResources())) {
mAutoBrightnessPreference = (SwitchPreference) findPreference(KEY_AUTO_BRIGHTNESS);
mAutoBrightnessPreference.setOnPreferenceChangeListener(this);
} else {
removePreference(KEY_AUTO_BRIGHTNESS);
}
===》可见对此开关设置了监听回调
其回调方法为:public boolean onPreferenceChange(Preference preference, Object objValue)
===》回调中处理开关逻辑:
if (preference == mAutoBrightnessPreference) {
boolean auto = (Boolean) objValue;
Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE,
auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
}
====================end=============================================================================
参考博客:
https://blog.csdn.net/wangjinyu501/article/details/22077803
https://blog.csdn.net/szqsdq/article/details/52537281
https://www.jianshu.com/p/691b2ad46e62
https://blog.csdn.net/wlwl0071986/article/details/47805163?utm_source=blogkpcl13
https://blog.csdn.net/u013921245/article/details/50607460?utm_source=blogxgwz0