2017-05-05 25 views
0

我在我的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; 
    } 
} 

回答

0

您需要使用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

相關問題