[英]Android: combine PreferenceFragment with other widgets in single layout
I'm trying to have an Activity
that shows just a few (or one) preference item(s) and some more widgets in the remaining space. 我正在尝试在一个
Activity
中显示一些(或一个)首选项,并在剩余空间中显示一些小部件。 I'm having a hard time figuring out how to do this. 我很难搞清楚如何做到这一点。
in /res/xml/preferences_1.xml : 在/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>
The layout for the activity that I'd like to contain an ui to edit this single preference: /res/layout/test.xml : 我想要包含用于编辑此单个首选项的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>
And the Activity
: 和
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);
};
}
}
This should illustrate what I'm trying to achieve: having the <fragment>
show the PreferenceFragment
and below that still have a (or some) widget(s). 这应该说明我想要实现的目标:让
<fragment>
显示PreferenceFragment
并且下面仍然有一个(或某些)widget。
Sadly, this doesn't work as I had hoped. 可悲的是,这并不像我希望的那样有效。 Instead it throws when opening the activity.
相反,它会在打开活动时抛出。 The relevant part of the stack trace seems to be:
堆栈跟踪的相关部分似乎是:
E/AndroidRuntime(12584): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.something/com.test.something.TestActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
E / AndroidRuntime(12584):java.lang.RuntimeException:无法启动活动ComponentInfo {com.test.something / com.test.something.TestActivity}:android.view.InflateException:二进制XML文件行#7:错误膨胀类分段
Could someone show me how this is done? 有人能告诉我这是怎么做到的吗?
Argh! 哎呀! Later on in the stacktrace there was actually a much more helpful message.
后来在堆栈跟踪中实际上有一个更有帮助的消息。 The
<fragment>
was missing an android:id
attribute. <fragment>
缺少一个android:id
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.