自訂PreferenceActivity的樣式

來源:互聯網
上載者:User

寫一個系統的設定介面,將其設定樣式就可以了

 

 

 

先建一個SettingActivity工程:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.cn.nj.setting"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:label="@string/app_name"            android:name=".SettingActivity"             android:theme="@style/Default" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

在res/xml/setting_preference.xml

<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >    <PreferenceCategory android:title="訊息通知" >        <CheckBoxPreference            android:defaultValue="true"            android:key="開啟聲音"            android:summary="當有新訊息通知時播放聲音提示"            android:title="開啟聲音" />        <CheckBoxPreference            android:defaultValue="true"            android:key="開啟震動"            android:summary="當有新訊息通知時震動提示"            android:title="開啟震動" />    </PreferenceCategory>    <PreferenceCategory android:title="協助工具功能" >        <CheckBoxPreference            android:defaultValue="false"            android:key="開啟截屏功能"            android:summary="搖晃手機即可(在QQ外需root許可權)"            android:title="開啟截屏功能" />        <CheckBoxPreference            android:defaultValue="false"            android:key="上報我的地理位置"            android:summary="上報地理位置資訊以便附近的人能夠找到我"            android:title="上報我的地理位置" />    </PreferenceCategory>    <PreferenceCategory android:title="協助" >        <PreferenceScreen            android:summary="反饋建議"            android:title="反饋建議" >            <intent                android:action="android.intent.action.VIEW"                android:data="http://www.baidu.com" />        </PreferenceScreen>    </PreferenceCategory></PreferenceScreen>

在res/values/style.xml

<?xml version="1.0" encoding="utf-8"?><resources>         <style name="CustomWindowTitleText" >            <item name="android:textSize">20dip</item>            <item name="android:textColor">#FFffffff</item>            <item name="android:paddingLeft">10dp</item>     </style>             <style name="customCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">        <item name="android:button">@drawable/selector_checkbox</item>    </style>        <style name="Default.NoTitleBar" parent="@android:style/Theme.Light.NoTitleBar"><item name="android:textColorPrimaryInverse">@android:color/black</item>           <item name="android:windowBackground">@color/window_bg</item>        <item name="android:windowContentOverlay">@null</item>        <item name="android:windowTitleSize">42.0dip</item>        <item name="android:windowTitleStyle">@style/CustomWindowTitleText</item>         <item name="android:checkboxStyle">@style/customCheckBox</item>    </style>            <style name="Default" parent="@style/Default.NoTitleBar">        <item name="android:windowNoTitle">false</item>    </style>        </resources><!-- item name=android:textColorPrimaryInverse preference標題文本顏色--><!-- item name="android:windowBackground"表單背景--><!-- item name=android:windowTitleBackgroundStyle表單標題背景風格--><!-- item name="android:windowTitleSize"  表單標題列高度--><!-- item name="android:listViewStyle"preference是一個LISTVIEW,這裡設定該風格--><!-- item name="android:textColorPrimary"preference一級文本顏色--><!-- item name="android:textColorSecondary"preference二級文本顏色-->

在res/drawable/selector_checkbox.xml

<?xml version="1.0" encoding="UTF-8"?><selector  xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_checked="false" android:drawable="@drawable/checkbox_unchecked" />    <item android:state_checked="true" android:drawable="@drawable/checkbox_checked" /></selector>

在res/drawable/checkbox_checked.xml

<?xml version="1.0" encoding="UTF-8"?><layer-list  xmlns:android="http://schemas.android.com/apk/res/android">    <item>        <shape>            <solid android:color="#ffecf6fb" />            <stroke android:width="1.0dip" android:color="#ffb3cad9" />            <padding android:left="5.0dip" android:top="5.0dip" android:right="5.0dip" android:bottom="5.0dip" />        </shape>    </item>    <item>        <shape>            <solid android:color="#ff99c308" />            <padding android:left="5.0dip" android:top="5.0dip" android:right="5.0dip" android:bottom="5.0dip" />        </shape>    </item>    <item>        <shape>            <solid android:color="#ffffffff" />        </shape>    </item></layer-list>

在res/drawable/checkbox_unchecked.xml

<?xml version="1.0" encoding="UTF-8"?><shape android:shape="rectangle"  xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#ffecf6fb" />    <stroke android:width="1.0dip" android:color="#ffb3cad9" />    <size android:height="18.0dip" android:width="18.0dip" /></shape>

SettingActivity:

package com.cn.nj.setting;import android.os.Bundle;import android.preference.PreferenceActivity;public class SettingActivity extends PreferenceActivity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //setContentView(R.layout.main);        addPreferencesFromResource(R.xml.setting_preference);    }}

 

運行效果如下:

 

 代碼為:http://download.csdn.net/detail/niejing654092427/4409630

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.