setting-在设置中添加新的选项
如下图的“通知栏调出方式”
具体实现如下
1.在
res/xml/settings_headers.xml
文件中添加如下内容
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> <header android:id="@+id/quick_pulldown" android:fragment="com.android.settings.QuickPullDown" android:title="@string/quick_pulldown" android:icon="@drawable/ic_quick_pulldown" /> </preference-headers>
2.关联的fragment类如下
public class QuickPullDown extends SettingsPreferenceFragment implements OnPreferenceClickListener { private Context mContext; private AudioProfilePreference mOffPressPref; private AudioProfilePreference mRightPressPref; private AudioProfilePreference mLeftPressPref; private static final int QS_QUICK_PULLDOWN_DEFAULT_VALUE = 0; private static final String OFF = "off"; private static final String RIGHT = "right"; private static final String LEFT = "left"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = getActivity(); addPreferencesFromResource(R.xml.quick_pulldown); mOffPressPref = (AudioProfilePreference) findPreference(OFF); mRightPressPref = (AudioProfilePreference) findPreference(RIGHT); mLeftPressPref = (AudioProfilePreference) findPreference(LEFT); mOffPressPref.isShowDetailsView = false; mRightPressPref.isShowDetailsView = false; mLeftPressPref.isShowDetailsView = false; mOffPressPref.setOnPreferenceClickListener(this); mRightPressPref.setOnPreferenceClickListener(this); mLeftPressPref.setOnPreferenceClickListener(this); refreshPressState(); } @Override public void onResume() { super.onResume(); getListView().setDivider(null); getListView().setBackgroundResource(R.drawable.funui_background); getListView().setSelector(android.R.color.transparent); new BackgroundChooser().justDoIt(getPreferenceScreen()); } @Override public boolean onPreferenceClick(Preference preference) { if (preference instanceof AudioProfilePreference) { if (preference == mOffPressPref) { Settings.System.putInt(mContext.getContentResolver(), QS_QUICK_PULLDOWN, 0); } else if (preference == mRightPressPref) { Settings.System.putInt(mContext.getContentResolver(), QS_QUICK_PULLDOWN, 1); } else if (preference == mLeftPressPref) { Settings.System.putInt(mContext.getContentResolver(), QS_QUICK_PULLDOWN, 2); } refreshPressState(); } return true; } public void refreshPressState() { int value = Settings.System.getInt( getContentResolver(), QS_QUICK_PULLDOWN, QS_QUICK_PULLDOWN_DEFAULT_VALUE); mOffPressPref.setRadioButtonClickable(false); mRightPressPref.setRadioButtonClickable(false); mLeftPressPref.setRadioButtonClickable(false); switch (value) { case 0: mOffPressPref.setChecked(); break; case 1: mRightPressPref.setChecked(); break; case 2: mLeftPressPref.setChecked(); break; default: break; } } }
3.这个类中用到的布局文件如下
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <com.android.settings.FunuiPreferenceCategory android:key="quick_pulldown_setting" android:persistent="false" android:title="@string/quick_pulldown"> <com.mediatek.audioprofile.AudioProfilePreference android:key="off" android:layout="@layout/funui_switch_preference_full" android:title="@string/quick_pulldown_off" android:summary="@string/quick_pulldown_off_describe"/> <com.mediatek.audioprofile.AudioProfilePreference android:key="right" android:layout="@layout/funui_switch_preference_full" android:title="@string/quick_pulldown_right" android:summary="@string/quick_pulldown_right_describe"/> <com.mediatek.audioprofile.AudioProfilePreference android:key="left" android:layout="@layout/funui_switch_preference_full" android:title="@string/quick_pulldown_left" android:summary="@string/quick_pulldown_left_describe"/> </com.android.settings.FunuiPreferenceCategory> </PreferenceScreen>
这些其实就是setting这个模块的基本实现框架了
如何设定它的显示跟隐藏呢,主要是在这个
src/com/android/settings/Settings.java
类中实现的,代码如下
public void updateHeaderList(List<Header> target) { if (id == R.id.quick_pulldown) { target.remove(header); } }