我的TabLayout中有三个标签,并希望其中一个用于设置/首选项。因此,我尝试实施推荐的Preference UI构建块,将其添加到我的选项卡中作为片段。所以我创建了xml
目录,其中包含一个preferences.xml
文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="text"
android:title="text"
android:summary="text"
android:defaultValue="true" />
</PreferenceScreen>
访问设置标签时,我想扩展到我的模板活动:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.zorgan.app.Find">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="40dp" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
我目前通过Find.java
中的此代码打开所选标签的活动:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
case 3: {
rootView = inflater.inflate(R.layout.fragment_settings, container, false);
break;
}
}
return rootView;
}
}
然而,在我的switch语句的第三种情况中,当我将R.layout.fragment_settings
更改为R.xml.preferences.
时,会出现红线错误Expected resource of type layout
。知道它为什么不允许这个XML文件吗?如何使用case 3
转到R.xml.preferences
而不是常规布局XML文件?
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);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
case 3: {
SettingsFragment fragment = new SettingsFragment();
// what goes here?
break;
}
}
return rootView;
}
}
答案 0 :(得分:0)
您需要使用Preference fragment。
您不需要调用inflate视图;相反,你应该加载这样的偏好:
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);
}
...
}