请勿转载,辛苦之作,转载注明此文来自http://write.blog.csdn.net/postedit
此处针对android3.0以后,更加倾向于使用PreferenceFragment而不再使用PreferenceActivity,因为PreferenceFragment更加灵活,此处主要使用两个方法即可达到效果
本文采用ationbar配合PreferenceFragment的方式,在主页面的actionbar中点击跳转进入一个Activity’页面,然后在其中加载PreferenceFragment的方式,因为本文采用了actionbar,所以记住v7包的导入,现在进入正题
代码如下:
MainActivity代码:
public class MainActivity extends ActionBarActivity {
private static final int menu_setting = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,menu_setting,1,"进入PreferenceFragment").setIcon(android.R.drawable.ic_menu_preferences);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
Intent intent = new Intent(this, FragmentPreferences.class);
startActivity(intent);
return false;
}
}
FragmentPreferences代码如下:
public class FragmentPreferences extends Activity implements OnClickListener {
private Button one;
private Button two;
private LinearLayout content;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.item);
initView();
initListener();
}
private void initView() {
one = (Button) findViewById(R.id.oneFragment);
two = (Button) findViewById(R.id.twoFragment);
content = (LinearLayout) findViewById(R.id.content);
}
private void initListener() {
// TODO Auto-generated method stub
one.setOnClickListener(this);
two.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.oneFragment:
//此方法就是为了加载PreferenceFragment页面
getFragmentManager().beginTransaction().replace(R.id.content, new OneFragment()).commit();
break;
case R.id.twoFragment:
getFragmentManager().beginTransaction().replace(R.id.content, new TwoFragment()).commit();
break;
default:
break;
}
}
}
BaseFragment代码:
public abstract class BaseFragment extends PreferenceFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initView();
}
public abstract void initView() ;
}
//OneFragment 代码
public class OneFragment extends BaseFragment {
@Override
public void initView() {
// TODO Auto-generated method stub
addPreferencesFromResource(R.xml.preferences);
}
}
//twoFragment代码
public class TwoFragment extends BaseFragment {
@Override
public void initView() {
// TODO Auto-generated method stub
addPreferencesFromResource(R.xml.preferences1);
}
}
activity_main布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.preferencefragment.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
item布局文件
<?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="match_parent"
android:orientation="vertical"
android:id="@+id/ll">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
>
<Button
android:id="@+id/oneFragment"
android:text="oneFragment"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/twoFragment"
android:text="twoFragment"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/content"></LinearLayout>
</LinearLayout>
activity_fragment_preferences.xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.preferencefragment.FragmentPreferences" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
在res下创建文件夹xml 在其下面创建两个文件
preferences.xml布局如下:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="one" >
<CheckBoxPreference
android:key="checkbox"
android:summary="复选框1"
android:title="复选框设置" />
</PreferenceCategory>
<PreferenceCategory android:title="two" >
<EditTextPreference
android:dialogTitle="请输入你最喜欢的男神"
android:key="nanshen"
android:summary="一个使用了编辑文本对话框的例子"
android:title="男神" />
<ListPreference
android:entries="@array/name"
android:entryValues="@array/name_values"
android:dialogTitle="请选择一项"
android:key="list_preferenc"
android:summary= "列表对话框"
android:title="请选择一项" />
</PreferenceCategory>
</PreferenceScreen>
preferences1.xml布局如下:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="one" >
<CheckBoxPreference
android:key="checkone"
android:summary="复选框2"
android:title="复选框设置" />
</PreferenceCategory>
<PreferenceCategory android:title="two" >
<EditTextPreference
android:dialogTitle="请输入你最喜欢的女神"
android:key="nvshen"
android:summary="一个使用了编辑文本对话框的例子"
android:title="女神" />
<ListPreference
android:entries="@array/name"
android:entryValues="@array/name_values"
android:dialogTitle="请选择一项"
android:key="onelist"
android:summary= "列表对话框"
android:title="请选择一项" />
</PreferenceCategory>
</PreferenceScreen>
在values文件夹下创建arrays.xml 内容如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="name">
<item>咪咪</item>
<item>大咪咪</item>
<item>小咪咪</item>
</string-array>
<string-array name="name_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>
和大家一起交流交流