在4.0中使用 PreferenceActivity 的addPreferencesFromResource(R.xml.*); 提示过期,经查证,新的替代为 PreferenceFragment,所以,打算将之前的代码换掉。
以前的代码结构:
public class MainActivity extends PreferenceActivity {
@SuppressWarnings({ "deprecation" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
addPreferencesFromResource(R.xml.main_settings);
}
}
官方给出的新的代码结构为:
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
}
执行一下,效果盖住了整个屏幕,并不是自己想要的(自己想把preference嵌入其他布局中)。由于学艺不精,不知道 android.R.id.content 是个什么元素。今天突然发现它原来是根布局。有点小高兴,遂做了简单的修改:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Holo_Light_NoActionBar);
setContentView(R.layout.activity_main);
// getFragmentManager().beginTransaction()
// .replace(android.R.id.content, new PrefsFragement()).commit();
getFragmentManager().beginTransaction()
.replace(R.id.llPF, new PrefsFragement()).commit();
}
public static class PrefsFragement extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
布局文件activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvAlert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="测试 Preferencefragement" />
<LinearLayout
android:id="@+id/llPF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvPF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dip"
android:text="我是llPF的子元素" />
</LinearLayout>
</LinearLayout>
最终效果如下:
虽然效果实现,但是并没有达到和之前一样的效果,尝试修改Activity的主题,但是不起作用,请高手指点
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ttdevs.test.preferencefragement"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
参考:
http://developer.android.com/guide/topics/ui/settings.html#Fragment