Android PreferenceScreen介绍
源代碼:
http://download.csdn.net/detail/zhuimengandyue/5914779
如上圖所示效果,實現(xiàn)起來很簡單。只需要借助PreferenceScreen類即可。在項目資源文件中新建xml文件夾,在里面新建preferences.xml文件.
根元素為PreferenceScreen 代表顯示一整個屏幕,內部嵌套PreferenceCategory標簽,表示偏好類別,在PreferenceCategory標簽內部可以隨便存放復 選框,輸入框,列表等顯示控件.可包含的控件內容在android.preference包下可查閱.xml文件編寫好后,需要加載到activity 中,對于偏好顯示的xml加載,可以使用PreferenceActivity中的addPreferencesFromResource(),所以 Activity需要繼承PreferenceActivity.
復寫activity中的onPreferenceTreeClick 方法,在對屏幕顯示的內容進行操作時,會觸發(fā)此方法,在方法內部通過調用。
preference.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
??? >
??? <PreferenceCategory android:title="Jamendo"? >
??????? <CheckBoxPreference
??????????? android:key="checkbox"
??????????? android:summary="Listen only in wifi network area"
??????????? android:title="Wifi only mode" />
??????? <EditTextPreference
??????????? android:dependency="checkbox"
??????????? android:key="edittextpreffer"
??????????? android:summary="Personallize your jamendo client"
??????????? android:title="User name" />
??????? <ListPreference
??????????? android:dependency="checkbox3"
??????????? android:entries="@array/stream_codecs"
??????????? android:entryValues="@array/stream_codecs_values"
??????????? android:key="listpreffer"
??????????? android:summary="select your preferred codec"
??????????? android:title="Download format" />
??????? <CheckBoxPreference
??????????? android:disableDependentsState="true"
??????????? android:key="checkbox3"
??????????? android:summaryOn="Disable listening abroad"
??????????? android:title="Disable listening abroad" />
??? </PreferenceCategory>
??? <PreferenceCategory
??????? android:summary="lalalalalalalla"
??????? android:title="3rd Party Applications" >
??????? <CheckBoxPreference
??????????? android:key="checkbox2"
??????????? android:summaryOff="fdsfsddddddddddddddddddddddddddddddd"
??????????? android:title="fdsfsafsdfdsfds" />
??????? <CheckBoxPreference
??????????? android:key="checkbo"
??????????? android:summaryOff="fdsfsddddddddddddddddddddddddddddddd"
??????????? android:title="fdsfsafsdfdsfds" />
??????? <RingtonePreference
??????????? android:key="rengtone"
??????????? android:summary="select rengtone"
??????????? android:title="rengtones" />
??????? <Preference
??????????? android:title="tiantianxiangshang"/>
??? </PreferenceCategory>
</PreferenceScreen>
setting.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:layout_width="fill_parent"
??? android:layout_height="fill_parent"
??? android:orientation="vertical" >
??? <LinearLayout
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:gravity="center"
??????? android:minHeight="50dip"
??????? android:orientation="horizontal" >
??????? <LinearLayout
??????????? android:layout_width="fill_parent"
??????????? android:layout_height="wrap_content"
??????????? android:gravity="center_vertical"
??????????? android:minHeight="50dip"
??????????? android:orientation="horizontal"
??????????? android:paddingLeft="13dip"
??????????? android:background="#4b0082"
??????????? android:paddingRight="13dip" >
??????????? <ImageView
??????????????? android:layout_width="48dip"
??????????????? android:layout_height="48dip"
??????????????? android:src="@drawable/ic_launcher" >
??????????? </ImageView>
??????????? <LinearLayout
??????????????? android:layout_width="fill_parent"
??????????????? android:layout_height="wrap_content"
??????????????? android:orientation="vertical"
??????????????? android:paddingLeft="13dip" >
??????????????? <TextView
??????????????????? android:layout_width="wrap_content"
??????????????????? android:layout_height="wrap_content"
??????????????????? android:singleLine="true"
??????????????????? android:text="setting"
??????????????????? android:textColor="#ffffff"
??????????????????? android:textSize="18dip" >
??????????????? </TextView>
??????????????? <TextView
??????????????????? android:layout_width="wrap_content"
??????????????????? android:layout_height="wrap_content"
??????????????????? android:textColor="#ffffff"
??????????????????? android:textSize="12dip" >
??????????????? </TextView>
??????????????? <TextView
??????????????????? android:id="@+id/ItemsCountTextView"
??????????????????? android:layout_width="wrap_content"
??????????????????? android:layout_height="wrap_content"
??????????????????? android:layout_gravity="right"
??????????????????? android:textColor="#ffffff"
??????????????????? android:textSize="12dip" >
??????????????? </TextView>
??????????? </LinearLayout>
??????? </LinearLayout>
??? </LinearLayout>
??? <ListView
??????? android:id="@android:id/list"
??????? android:layout_width="fill_parent"
??????? android:layout_height="fill_parent"
??????? android:layout_weight="1"
??????? android:scrollingCache="false">
??? </ListView>
</LinearLayout>
java代碼:
package com.example.test;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.view.Window;
public class SettingActivity extends PreferenceActivity{
?@Override
?protected void onCreate(Bundle savedInstanceState) {
??// TODO Auto-generated method stub
??requestWindowFeature(Window.FEATURE_NO_TITLE);
??super.onCreate(savedInstanceState);
??addPreferencesFromResource(R.xml.preference);
??setContentView(R.layout.setting);
?}
?@Override
?public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
???Preference preference) {
??// TODO Auto-generated method stub
??return super.onPreferenceTreeClick(preferenceScreen, preference);
?}
}
重要的幾個屬性:1、android:dependency="config_anonymous"這個屬性指定該設置項依賴于前面key="config_anonymous" 的CheckBoxPreference的值,
如果config_anonymous的值為真(即選中狀態(tài)),則下面的設置項可用,即enabled的,否則為disabled。
但是,如果邏輯剛好相反,即config_anonymous的值為真的時候,需要禁用下面的設置項。要實現(xiàn)這個就需要在CheckBoxPreference上設置了,
即添加android:disableDependentsState="true"這個屬性,這個屬性的意思是,當CheckBoxPreference的值為真的時候
2、使用PreferenceActivity時,碰到配置文件的ListPreference有兩個屬性android:entries,android:entryValues。這兩個屬性其實就和html的option的顯示內容和真實值一樣。
android:entries設置的內容是我們在設置時看到的內容,而android:entryValues就是保存在preferences.xml中的值。
?
?
?
總結
以上是生活随笔為你收集整理的Android PreferenceScreen介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 霍尼韦尔从纽交所转板至纳斯达克上市;全球
- 下一篇: OpenMP与C++ 事半功倍地获得多线