本博文主要讲述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文件