0

我正在尝试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和下方仍然有一个(或一些)小部件。

可悲的是,这并没有像我希望的那样工作。相反,它在打开活动时抛出。堆栈跟踪的相关部分似乎是:

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分段

有人可以告诉我这是怎么做到的吗?

4

1 回答 1

2

啊!后来在堆栈跟踪中,实际上有一条更有帮助的消息。<fragment>缺少一个android:id属性。

于 2012-05-19T13:15:03.190 回答