亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

使用PreferenceActivity和PreferenceScreen構(gòu)建

系統(tǒng) 2395 0

?? 對于每個(gè)應(yīng)用程序來說,都要有一些屬于用戶自己的設(shè)置,滿足不同需求。當(dāng)我們點(diǎn)擊menu時(shí),如下:



?點(diǎn)擊settings時(shí),出現(xiàn):


使用PreferenceActivity和PreferenceScreen構(gòu)建應(yīng)用的設(shè)置
?那么這樣的效果是怎么實(shí)現(xiàn)的呢?我只是來個(gè)簡單介紹,給自己做備忘,也是給大家點(diǎn)思路吧。在android的路上,我們一起努力吧。

這里我們僅說第二個(gè)圖片效果的實(shí)現(xiàn),第一個(gè)圖片的效果,想必大家都會了,就是使用menu類的幾個(gè)方法就可以了。

1.PreferenceScreen 的使用。

首先要定義一下整個(gè)布局即使用xml文件夾下的preferences.xml。

代碼如下:

    <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 如果擁有多個(gè)首選項(xiàng),可以構(gòu)建一個(gè)視圖來顯示首選項(xiàng)高級類別。用戶然后就可以深入到每個(gè)類別,查看和管理特
定于該組的首選項(xiàng)。 可以通過兩種方式來實(shí)現(xiàn)此目的。可以在根 PreferenceScreen中引入嵌套的 PreferenceScreen 元素,
或者可以使用 PreferenceCategory 來獲得類似的結(jié)果。 -->
	<PreferenceCategory android:title="@string/app_name"
		android:summary="Application settings">

		<EditTextPreference android:key="user_name"
			android:defaultValue="@null" 
			android:title="@string/preference_username_title"
			android:summary="@string/preference_username_summary" />
		<ListPreference
                android:key="download_format"
                android:title="@string/preference_codec_title"
                android:summary="@string/preference_codec_summary"
                android:entries="@array/stream_codecs"
                android:entryValues="@array/stream_codecs_values" />
		<ListPreference
                android:key="cache_option"
                android:title="@string/preference_cache_title"
                android:summary="@string/preference_cache_summary"
                android:entries="@array/cache_size"                 
                android:entryValues="@array/cache_size_values"/>                
		<CheckBoxPreference android:key="wifi_only"
			android:defaultValue="false" 
			android:title="@string/preference_wifi_only_title"
			android:summary="@string/preference_wifi_only_summary" />
		<CheckBoxPreference android:key="roaming_protection"
			android:defaultValue="true" 
			android:title="@string/preference_roaming_summary"
			android:summary="@string/preference_roaming_summary" />
		
	</PreferenceCategory>

	<PreferenceCategory android:title="@string/preference_3rdparty_title"
		android:summary="@string/preference_3rdparty_summary">

		<CheckBoxPreference android:defaultValue="false" 
			android:key="scrobbling_enabled" android:title="@string/scrobbling_enabled"/>
		<ListPreference android:key="scrobbler_app" android:dependency="scrobbling_enabled" android:entries="@array/scrobbler_apps" android:title="@string/scrobbler_app" android:entryValues="@array/scrobbler_apps_values" android:summary="@string/scrobbler_app_summary"></ListPreference>
		
	</PreferenceCategory>

	<PreferenceCategory android:title="@string/gestures_preference_title"
		android:summary="@string/gestures_preference_summary">
		<CheckBoxPreference android:key="gestures"
			android:defaultValue="true"
			android:title="@string/gestures_support"
			android:summary="@string/gestures_support_summary"/>
	</PreferenceCategory>
	<PreferenceCategory android:title="@string/preference_reset_title">
		<Preference android:key="reset_firstrun" android:summary="@string/preference_firstrun_reset_summary" android:title="@string/preference_firstrun_reset_title"></Preference>
	</PreferenceCategory>

</PreferenceScreen>
  

?

其中:

特性??????????????????????????????? 說明
android:key?????????????????????? 選項(xiàng)的名稱或鍵(比如selected_flight_sort_option)

android:title?????????????????????? 選項(xiàng)的標(biāo)題

android:summary?????????????? 選項(xiàng)的簡短摘要

android:entries????????????????? 可將選項(xiàng)設(shè)置成列表項(xiàng)的文本

android:entryValues????????? 定義每個(gè)列表項(xiàng)的值。注意:每個(gè)列表項(xiàng)有一些文本和 一 個(gè) 值。 文本由

entries定義,值由entryValues定義。

android:dialogTitle???????????? 對話框的標(biāo)題,在視圖顯示為模態(tài)對話框時(shí)使用
android:defaultValue????????? 項(xiàng)列表中選項(xiàng)的默認(rèn)值

?

在開發(fā)文檔中這樣定義PreferenceScreen,?Represents a top-level Preference that is the root of a Preference hierarchy. 即顯示了首選項(xiàng)組織體系的最頂級。

?

2.PreferenceActivity

A PreferenceActivity points to an instance of this class to show the preferences. 即我們通過實(shí)例化一個(gè)繼承PreferenceActivity 的類來展示首選項(xiàng),代碼如下

    public class SettingsActivity extends PreferenceActivity { 
@Override
 public void onCreate(Bundle savedInstanceState) 
  
    { requestWindowFeature(Window.FEATURE_NO_TITLE); 
  
    super.onCreate(savedInstanceState); //添加設(shè)置,加入引用 
  
    addPreferencesFromResource(R.xml.preferences); 
setContentView(R.layout.settings); }
  

?

?關(guān)于布局文件settings.xml:

?

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:background="#000000">

	<LinearLayout android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:background="@drawable/gradient_dark_purple"
		android:orientation="horizontal" android:gravity="center"
		android:minHeight="75dip">
		<LinearLayout android:layout_height="wrap_content"
			android:minHeight="75dip" android:layout_width="fill_parent"
			android:orientation="horizontal" android:gravity="center_vertical"
			android:paddingLeft="13dip" android:paddingRight="13dip">
			<ImageView android:layout_width="48dip"
				android:layout_height="48dip" android:src="@drawable/settings"></ImageView>
			<LinearLayout android:layout_height="wrap_content"
				android:paddingLeft="13dip" android:orientation="vertical"
				android:layout_width="fill_parent">
				<TextView android:layout_width="wrap_content"
					android:singleLine="true" android:layout_height="wrap_content"
					android:textSize="18dip" android:textColor="#ffffff" android:text="@string/settings"></TextView>
				<TextView android:layout_width="wrap_content"
					android:layout_height="wrap_content" android:textSize="12dip"
					android:textColor="#ffffff"></TextView>
				<TextView android:id="@+id/ItemsCountTextView"
					android:layout_width="wrap_content" android:layout_height="wrap_content"
					android:layout_gravity="right" android:textSize="12dip"
					android:textColor="#ffffff" android:text=" "></TextView>
			</LinearLayout>
		</LinearLayout>
	</LinearLayout>

	<ListView android:layout_width="fill_parent" android:id="@android:id/list"
		android:layout_weight="1" android:layout_height="fill_parent">
	</ListView>
</LinearLayout>
  

?當(dāng)我們點(diǎn)擊其中某一項(xiàng)時(shí),如cache option

效果:

?
使用PreferenceActivity和PreferenceScreen構(gòu)建應(yīng)用的設(shè)置
?用到了我們在res下定義的arrays.xml

其中部分內(nèi)容如下:

       <resources>   
  <string-array name="cache_size">
        <item>Off</item>
        <item>50 MB</item>
        <item>100 MB</item>
        <item>250 MB</item>
        <item>500 MB</item>
    </string-array>    
    <string-array name="cache_size_values">
        <item>0</item>
        <item>50</item>
        <item>100</item>
        <item>250</item>
        <item>500</item>
    </string-array>
</resources>
  

?

實(shí)在不明白的可以參考: http://byandby.iteye.com/blog/1045508 ?謝謝作者呵呵。

?


?僅提供思路。


?

使用PreferenceActivity和PreferenceScreen構(gòu)建應(yīng)用的設(shè)置


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲欧洲第一页 | 高清视频一区二区 | 欧美一级毛片不卡免费观看 | 日韩在线色 | 国产综合久久 | 九九影视理论片在线播放 | 亚欧中文字幕 | 国产精品二区高清在线 | 中文字幕在线免费观看视频 | 日韩欧美在线播放视频 | 国产一级免费在线观看 | 一级播放| 久久一本综合 | 91亚洲国产 | 色悠久久久久综合网小说 | 久久福利资源网站免费看 | 亚洲综合专区 | 国产色在线 | 精品午夜国产在线观看不卡 | 欧美精品成人免费视频 | 欧美日韩永久久一区二区三区 | 国产亚洲男人的天堂在线观看 | 免费一级毛片 | 国产一区二区三区在线观看免费 | 欧美亚洲国产精品第一页 | 日本一道一区 | 久久综合久久鬼色 | 狠狠躁夜夜躁人人爽天天不 | 99国产精品视频免费观看 | 色综合欧美综合天天综合 | 久久久免费| 中文字幕一区二区区免 | 欧美日韩激情在线一区二区 | 久久精品国产只有精品2020 | 精品日韩一区二区 | 欧美一级毛片在线一看 | 国产亚洲精品日韩综合网 | 欧美国产成人精品一区二区三区 | 波多野野结衣1区二区 | se在线观看 | 在线亚洲欧洲福利视频 |