[英]Android: combine PreferenceFragment with other widgets in single layout
我正在嘗試在一個Activity
中顯示一些(或一個)首選項,並在剩余空間中顯示一些小部件。 我很難搞清楚如何做到這一點。
在/res/xml/preferences_1.xml中 :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="test"
android:title="test"
android:defaultValue="false" />
</PreferenceScreen>
我想要包含用於編輯此單個首選項的UI的活動的布局: /res/layout/test.xml :
<?xml version="1.0" encoding="utf-8"?>
<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">
<fragment
android:name="com.test.something.TestActivity$PrefsFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<View ... stuff ... />
</LinearLayout>
和Activity
:
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static class PrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from XML resource
this.addPreferencesFromResource(R.xml.preference_login);
};
}
}
這應該說明我想要實現的目標:讓<fragment>
顯示PreferenceFragment
並且下面仍然有一個(或某些)widget。
可悲的是,這並不像我希望的那樣有效。 相反,它會在打開活動時拋出。 堆棧跟蹤的相關部分似乎是:
E / AndroidRuntime(12584):java.lang.RuntimeException:無法啟動活動ComponentInfo {com.test.something / com.test.something.TestActivity}:android.view.InflateException:二進制XML文件行#7:錯誤膨脹類分段
有人能告訴我這是怎么做到的嗎?
哎呀! 后來在堆棧跟蹤中實際上有一個更有幫助的消息。 <fragment>
缺少一個android:id
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.