前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android6.0源码分析之Settings(二)

Android6.0源码分析之Settings(二)

作者头像
fanfan
发布2022-05-07 14:54:21
4220
发布2022-05-07 14:54:21
举报
文章被收录于专栏:编程思想之路编程思想之路

本博文主要讲述6.0列表的加载

首先外层布局

代码语言:javascript
复制
setContentView(mIsShowingDashboard ?
                R.layout.settings_main_dashboard : R.layout.settings_main_prefs);

因为是显示的设置主界面,即mIsShowingDashboard为true,所以加载settings_main_dashboard布局

settings_main_dashboard.xml

代码语言:javascript
复制
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/main_content"
             android:layout_height="match_parent"
             android:layout_width="match_parent"
             />

接下来调用

代码语言:javascript
复制
switchToFragment(DashboardSummary.class.getName(), null, false, false,
                        mInitialTitleResId, mInitialTitle, false);

加载fragment,加载的是DashboardSummary

代码语言:javascript
复制
 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
            boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
        if (validate && !isValidFragment(fragmentName)) {
            throw new IllegalArgumentException("Invalid fragment for this activity: "
                    + fragmentName);
        }
        Fragment f = Fragment.instantiate(this, fragmentName, args);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.main_content, f);
        if (withTransition) {
            TransitionManager.beginDelayedTransition(mContent);
        }
        if (addToBackStack) {
            transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
        }
        if (titleResId > 0) {
            transaction.setBreadCrumbTitle(titleResId);
        } else if (title != null) {
            transaction.setBreadCrumbTitle(title);
        }
        transaction.commitAllowingStateLoss();
        getFragmentManager().executePendingTransactions();
        return f;
    }

将fragment加载在控件ID为main_content的位置

在DashboardSummary.java中rebuildUI(context)加载 UI

调用settingsactivity中的方法获取到所有设置项

代码语言:javascript
复制
 List<DashboardCategory> categories =
                ((SettingsActivity) context).getDashboardCategories(true);

该方法内部实现是解析dashboard_categories.xml文件,获取到所有的设置项

dashboard_categories.xml如下

代码语言:javascript
复制
<dashboard-categories
        xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- WIRELESS and NETWORKS -->
    <dashboard-category
            android:id="@+id/wireless_section"
            android:key="@string/category_key_wireless"
            android:title="@string/header_category_wireless_networks" >

        <!-- Wifi -->
        <dashboard-tile
                android:id="@+id/wifi_settings"
                android:title="@string/wifi_settings_title"
                android:fragment="com.android.settings.wifi.WifiSettings"
                android:icon="@drawable/ic_settings_wireless"
                />

        <!-- Bluetooth -->
        <dashboard-tile
                android:id="@+id/bluetooth_settings"
                android:title="@string/bluetooth_settings_title"
                android:fragment="com.android.settings.bluetooth.BluetoothSettings"
                android:icon="@drawable/ic_settings_bluetooth"
                />


       。。。。
     。。。。。
        。。。

所以如果想要添加一个设置项,只需要在 dashboard_categories.xml文件中添加一个<dashboard-tile控件

如果要删除一个设置项可以在SettingsActivity中的updateTilesList方法中将removetile设置为true,然后移除

每一个设置项为自定义的dashboard-tile,其布局文件为dashboard_category.xml

代码语言:javascript
复制
View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mDashboard,
                    false);

dashboard_category.xml如下

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/category"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/dashboard_category_padding_start"
        android:paddingEnd="@dimen/dashboard_category_padding_end"
        android:orientation="vertical"
        android:background="@color/card_background"
        android:layout_marginBottom="8dip"
        android:elevation="@dimen/dashboard_category_elevation">

    <TextView android:id="@+id/category_title"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dashboard_category_title_height"
            android:paddingStart="@dimen/dashboard_category_title_margin_start"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:gravity="center_vertical"
            android:textAppearance="@style/TextAppearance.CategoryTitle"
            android:textAlignment="viewStart"
            />

    <com.android.settings.dashboard.DashboardContainerView
            android:id="@+id/category_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

</LinearLayout>

如果想要修改所有设置项的布局,可以修改dashboard_category.xml文件

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档