2014-01-30 66 views
7

我有左窗格和右窗格的多窗格視圖。在正確的片段上啓動一個PreferenceFragment。問題是片段看起來完全扭曲而沒有任何風格。有沒有辦法只將主題應用於PreferenceFragment?將自定義主題應用於PreferenceFragment

我試圖this,但沒有奏效

我的代碼

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    // create ContextThemeWrapper from the original Activity Context with the custom theme 
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.AppTheme_PreferenceTheme); 

    // clone the inflater using the ContextThemeWrapper 
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); 


    View view = super.onCreateView(localInflater, container, savedInstanceState); 

    return view; 

} 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    addPreferencesFromResource(R.xml.app_settings_preference_layout); 
} 

我認爲解決的辦法沒有奏效,因爲我已經在膨脹的onCreate優先佈局。有沒有一種方法可以在不使用addPreferencesFromResource方法和僅使用LayoutInflater服務的情況下膨脹偏好佈局?

回答

-2

你不需要使用onCreateView()

,您可以:

1)定義您的活動XML例如,您的應用程序佈局和片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
..blabla 
<LinearLayout 
    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:name="com.xxx.MyPreferencesFragment1" //your own extended class 
     android:id="@+id/preference_fragment1"/> 
    <fragment 
     android:name="com.xxx.MyPreferencesFragment2" //your own extended class 
     android:id="@+id/preference_fragment2"/> 
</LinearLayout> 
</RelativeLayout> 

public class MyPreferencesFragment1 extends PreferenceFragment { 
    // Required empty public constructor 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Load the preferences from an XML resource 
    addPreferencesFromResource(R.xml.preferences1); 
您可以:

2)只定義在活動XML的佔位符/容器佈局(「R.id.fragments_layout」),並從Java代碼

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/fragments_layout"> 
// only layout - dont define fragments this case 

和OFC現在你還需要擡高做休息此佈局容器內的偏好xml文件

getFragmentManager().beginTransaction().replace/add(R.id.fragments_layout, new MyPreferencesFragment1()).commit(); 
getFragmentManager().beginTransaction().replace/add(R.id.fragments_layout, new MyPreferencesFragment2()).commit();