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

本文深入探讨了Android 6.0 Settings应用中列表的加载过程,从settings_main_dashboard.xml的外层布局开始,详细介绍了如何通过DashboardSummary.java的rebuildUI方法加载UI,并在SettingsActivity中获取所有设置项。讲解了dashboard_categories.xml的结构以及如何在updateTilesList方法中删除设置项,通过设置removetile为true来实现移除操作。每个设置项使用自定义组件dashboard_category.xml进行布局。
摘要由CSDN通过智能技术生成

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

首先外层布局

setContentView(mIsShowingDashboard ?
                R.layout.settings_main_dashboard : R.layout.settings_main_prefs);

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

settings_main_dashboard.xml

<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"
             />


接下来调用

switchToFragment(DashboardSummary.class.getName(), null, false, false,
                        mInitialTitleResId, mInitialTitle, false);

加载fragment,加载的是DashboardSummary

 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中的方法获取到所有设置项

 List<DashboardCategory> categories =
                ((SettingsActivity) context).getDashboardCategories(true);
该方法内部实现是解析dashboard_categories.xml文件,获取到所有的设置项

dashboard_categories.xml如下

<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

View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mDashboard,
                    false);
dashboard_category.xml如下

<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文件






评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值