Android程序《设置》学习记录
A.在3.0版本之前,采用的是prefenceActivity
在MainActivity的oncreate()里面加入
addPreferencesFromResource(R.xml.main_headers);//加载配置文件进来
这部分网上很多介绍,界面很快就能搭建出来了。
wifi的设置介绍为例子
MainSetting----->继承自PreferenceActivity
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.main_headers); //加载配置文件进来
}
main_headers.xml----->标红部分是用于自定义的IconPreferenceScreen的加载,com.android.setting要换成你自己的包
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" xmlns:coomix="http://schemas.android.com/apk/res/com.android.setting" > <com.android.setting.IconPreferenceScreen android:key="wifi_header" android:summary="@string/wifi_summary" android:title="@string/wifi_settings" coomix:image="@drawable/wifiicon" > <intent android:action="android.intent.action.MAIN" android:targetClass="com.android.setting.WifiSetting" android:targetPackage="com.android.setting" > </intent> </com.android.setting.IconPreferenceScreen> <!-- android:summary="@string/wifi_summary" --> <com.android.setting.IconPreferenceScreen android:key="bluetooth_header" android:summary="@string/bluetooth_summary" android:title="@string/bluetooth_settings" coomix:image="@drawable/bluetoothicon" > <intent android:action="android.intent.action.MAIN" android:targetClass="com.android.setting.BlueToothSetting" android:targetPackage="com.android.setting" > </intent> </com.android.setting.IconPreferenceScreen> <!-- android:summary="@string/bluetooth_summary" --> <com.android.setting.IconPreferenceScreen android:key="gps_header" android:summary="@string/gps_summary" android:title="@string/gps_settings" coomix:image="@drawable/gpsicon" > <intent android:action="android.intent.action.MAIN" android:targetClass="com.android.setting.GpsSetting" android:targetPackage="com.android.setting" > </intent> </com.android.setting.IconPreferenceScreen> <!-- android:summary="@string/gps_summary" --> <Preference android:summary="Launches an Intent." android:title="Intent" > <intent android:action="android.intent.action.VIEW" android:data="http://www.baidu.com" /> </Preference> </PreferenceScreen>
IconPreferenceScreen.java----->自定义带图标的配置项
package com.android.setting; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.preference.Preference; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.ImageView; public class IconPreferenceScreen extends Preference { private static final String TAG = "IconPreferenceScreen"; private Drawable mIcon; public IconPreferenceScreen(Context context, AttributeSet attrs) { this(context, attrs, 0); } public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setLayoutResource(R.layout.summary_item);//
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Preference, defStyle, 0);//读取自定义的配置加载图片
mIcon = a.getDrawable(R.styleable.Preference_image);// Preference_image 注意我配置文件的写法
} @Override public void onBindView(View view) { super.onBindView(view); ImageView imageView = (ImageView) view.findViewById(R.id.image); if (imageView != null && mIcon != null) { imageView.setImageDrawable(mIcon);//从xml文件读取到image设置图片来源,
} } }
当然你需要自己配置R.styleable.Preference,这个在value文件夹下配置。
array.xml----->我写在这里了,注意我的写法
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Preference"> <attr name="image" format="reference" /> </declare-styleable> </resources>
summary_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="55dip" android:orientation="horizontal" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical" android:paddingLeft="15dip" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical" android:layout_marginBottom="6dip" android:layout_marginLeft="2dip" android:layout_marginRight="6dip" android:layout_marginTop="6dip" android:layout_weight="1" > <TextView android:id="@+android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical" android:ellipsize="marquee" android:fadingEdge="horizontal" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge" android:textSize="22sp" /> <TextView android:id="@+android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@android:id/title" android:layout_below="@android:id/title" android:layout_gravity="center_horizontal|center_vertical" android:maxLines="2" android:textAppearance="?android:attr/textAppearanceSmall" android:textSize="10dip" /> </RelativeLayout> </LinearLayout>
大致流程写完了,其他的按照wifi的设置就行了。
B.在3.0版本之后,采用的PreferenceActivity+PreferenceFragment方式
在MainActivity.java中
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.main_headers, target);
}
点击相应的fragment就可以自动根据配置文件进入不同的界面设置
wifi的设置介绍为例子
main_headers.xml----->WifiSetting、BlueToothSetting、GpsSetting等是自定义的Fragment
<?xml version="1.0" encoding="utf-8"?> <preference-headers xmlns:android="http://schemas.android.com/apk/res/android" > <header android:fragment="com.android.setting.WifiSetting" android:icon="@drawable/wifiicon" android:title="@string/wifi_settings" android:summary="@string/wifi_summary" /> <header android:fragment="com.android.setting.BlueToothSetting" android:icon="@drawable/bluetoothicon" android:title="@string/bluetooth_settings" android:summary="@string/bluetooth_summary" /> <header android:fragment="com.android.setting.GpsSetting" android:icon="@drawable/gpsicon" android:title="@string/gps_settings" android:summary="@string/gps_summary" /> <header android:title="Intent" android:summary="Launches an Intent."> <intent android:action="android.intent.action.VIEW" android:data="http://www.baidu.com" /> </header> </preference-headers>
WifiSetting.java:
package com.android.setting; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.widget.Toast; public class WifiSetting extends PreferenceFragment implements /*OnPreferenceChangeListener, */OnPreferenceClickListener { CheckBoxPreference mEditPrefs; PreferenceCategory a; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.wifi_fragment); mEditPrefs= (CheckBoxPreference)findPreference("wifi_switch"); /*mEditPrefs.setOnPreferenceChangeListener(this);*/ mEditPrefs.setOnPreferenceClickListener(this); a= (PreferenceCategory)findPreference("wifi"); } /*@Override public boolean onPreferenceChange(Preference preference, Object newValue) { return false; }*/ @Override public boolean onPreferenceClick(Preference preference) { // TODO Auto-generated method stub return false; } private void showToast(String arg){ Toast.makeText(getActivity(), arg, Toast.LENGTH_SHORT).show(); } }
WifiSetting.xml:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="@string/wifi_settings" > <ListPreference android:dialogTitle="@string/auto_update_frequency_title" android:entries="@array/auto_update_frequency_entry" android:entryValues="@array/auto_update_frequency_value" android:key="@string/auto_update_frequency_key" android:summary="@string/auto_update_frequency_summary" /> <PreferenceCategory android:key="wifi" > <CheckBoxPreference android:key="@string/wifi_switch_key" android:summaryOff="@string/wifi_switch_summary_off" android:summaryOn="@string/wifi_switch_summary_on" android:title="@string/wifi_switch_title" /> </PreferenceCategory> </PreferenceScreen>
就这样界面就可以出来了o(∩_∩)o ,当然其他几个的界面需要自己按照wifi的设置写出来。
现在两个界面我都实现了~~~~
后面测试响应事件中~~~~
(找不到string的资源文件自己去strings.xml配置把)