如何将我的preferences.xml链接到TabLayout中的选项卡?

问题描述:

我在我的TabLayout中有三个选项卡,并希望其中一个用于设置/首选项。所以我试图实现推荐的Preference UI构建块,以添加到我的选项卡中作为片段。所以,我创建了xml目录中包含一个preferences.xml文件看起来像这样:如何将我的preferences.xml链接到TabLayout中的选项卡?

<?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> 

我目前在第三种情况下,我switch语句

@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; 
    } 
} 

然而:通过这个代码在我Find.java打开所选选项卡的活动,当我将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; 
    } 
} 

您需要使用Preference fragment

您不需要调用充气视图;相反,你应该加载像这样的偏好:

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); 
    } 
    ... 
} 
+0

此代码去哪里? – Zorgan

+0

创建一个名为SettingsFragment的新片段,对于案例3,使用片段。 –

+0

不知道我是否做得对,但我在编辑中添加了我的代码。如果我迄今为止做得很好,在情况3下我怎么实际调用/启动'SettingsFragment'? – Zorgan