Android5.1系统下的Setting应用源码分析

本文深入剖析了Android系统5.1版本设置应用的源码结构,从Settings.java入口开始,详细介绍了设置界面的布局原理,Fragment的切换机制,以及如何加载和显示设置项。特别关注了DisplaySettings类,解释了自动亮度调节开关的工作流程。
摘要由CSDN通过智能技术生成

====================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
   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值