Android Studio爲開發者提供了不少內建的Activity, 其中Settings Activity是很是有用且功能強大的一種內建Activity.html
Setting Activity其實本質上是從PreferenceActivity中繼承過來的。使用Setting Activity後,徹底不須要本身控制Preferences的讀寫,PreferenceActivity會幫咱們處理一切。java
PreferenceActivity和普通的Activity不一樣,它再也不使用普通的界面佈局文件,而是使用選項設置的佈局文件。選項設置的佈局文件以PreferenceScreen做爲根元素,每個PreferenceScreen對應後臺的一個PreferenceFragment。android
使用Android Studio添加一個Activity,會默認幫咱們生成一個Pref_header.xml文件和若干個Pref*.xml文件。對應到Activity裏,須要對應定義幾個PreferenceFragment和重寫onBuildHeaders方法用於載入定義在Pref_header.xml中的入口布局。api
相應的代碼片斷以下app
Pref_header.xmlide
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> <!-- These settings headers are only used on tablets. --> <header android:fragment="com.example.xpshen.myapplication.SettingsActivity$GeneralPreferenceFragment" android:icon="@drawable/ic_info_black_24dp" android:title="@string/pref_header_general" /> <header android:fragment="com.example.xpshen.myapplication.SettingsActivity$NotificationPreferenceFragment" android:icon="@drawable/ic_notifications_black_24dp" android:title="@string/pref_header_notifications" /> <header android:fragment="com.example.xpshen.myapplication.SettingsActivity$DataSyncPreferenceFragment" android:icon="@drawable/ic_sync_black_24dp" android:title="@string/pref_header_data_sync" /> </preference-headers>
Pref_general.xml佈局
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <SwitchPreference android:defaultValue="true" android:key="example_switch" android:summary="@string/pref_description_social_recommendations" android:title="@string/pref_title_social_recommendations" /> <!-- NOTE: EditTextPreference accepts EditText attributes. --> <!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. --> <EditTextPreference android:capitalize="words" android:defaultValue="@string/pref_default_display_name" android:inputType="textCapWords" android:key="example_text" android:maxLines="1" android:selectAllOnFocus="true" android:singleLine="true" android:title="@string/pref_title_display_name" /> <!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to dismiss it. --> <!-- NOTE: ListPreference's summary should be set to its value by the activity code. --> <ListPreference android:defaultValue="-1" android:entries="@array/pref_example_list_titles" android:entryValues="@array/pref_example_list_values" android:key="example_list" android:negativeButtonText="@null" android:positiveButtonText="@null" android:title="@string/pref_title_add_friends_to_messages" /> </PreferenceScreen>
SettingActivity.javaui
public class SettingsActivity extends AppCompatPreferenceActivity { ... @Override @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void onBuildHeaders(List<Header> target) { loadHeadersFromResource(R.xml.pref_headers, target); } ... @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static class GeneralPreferenceFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.pref_general); setHasOptionsMenu(true); // Bind the summaries of EditText/List/Dialog/Ringtone preferences // to their values. When their values change, their summaries are // updated to reflect the new value, per the Android Design // guidelines. bindPreferenceSummaryToValue(findPreference("example_text")); bindPreferenceSummaryToValue(findPreference("example_list")); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { startActivity(new Intent(getActivity(), SettingsActivity.class)); return true; } return super.onOptionsItemSelected(item); } } }
這樣一個setting activity就能夠工做了。this
可是此類activity因爲不使用普通的界面佈局文件,咱們沒法在佈局文件中添加自定以的控件。spa
好比咱們想要在頁面的底部添加一個任務欄,實際上是沒法簡單的經過修改佈局文件來增長的。
本文采用的方法是基於下面文章的思路來的。
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0331/1608.html
基本的思路是,在Setting Activity的onCreate方法中,截獲以前佈局樹上的content元素,插入咱們自定義的底部任務欄。
代碼以下
SettingActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setupActionBar(); hookThebottomBar(); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); } private void hookThebottomBar(){ ViewGroup content = (ViewGroup) findViewById(android.R.id.content); LayoutInflater.from(this).inflate(R.layout.com_bottombar, content, true); }
com_bottombar.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom"> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?android:attr/windowBackground" app:menu="@menu/navigation" /> </LinearLayout>
這裏注意,咱們攔截到的content實際上是Framelayout,而咱們的目標是添加一個底部任務欄,因此須要在上面的com_bottombar.xml設置 android:layout_gravity="bottom",這樣這個咱們後續添加的幀纔不會覆蓋以前的內容。
最後的效果圖以下