如何在布局中包含偏好设置

4

我可以像在另一个布局中使用<include layout=""/>标签一样,在布局中包含偏好设置吗?

我的布局代码如下:

<LinearLayout 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:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:padding="@dimen/padding_medium"
    android:text="@string/hello_world"
    tools:context=".MainActivity" />

<include layout="@layout/settings"/>
<!-- <include layout="@xml/set_variables"/> -->
</LinearLayout>

在上面的布局中,我试图包含首选项@xml/set_variables,但是如果我取消注释<include layout="@xml/set_variables"/>,我的应用程序将崩溃。请问有什么办法可以在布局中包含首选项,还是我应该放弃?
1个回答

2
我浏览了这个链接,并找到了解决方案。在PreferenceActivityonCreate方法中,我添加了以下内容:
setContentView(R.layout.settings);
addPreferencesFromResource(R.xml.set_variables);

并在设置布局中添加了ListView

    <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

它符合我的要求


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接