2015-06-21 56 views
0

我想加載喜好到我的片段,它的工作,但我似乎無法找到一種方式來改變佈局。現在,我有這樣的問題:在片段重疊Android設置

enter image description here

這是我的代碼:

public class SettingsFragment extends Fragment { 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     mParam1 = getArguments().getString(ARG_PARAM1); 
     mParam2 = getArguments().getString(ARG_PARAM2); 
    } 
    //getFragmentManager().beginTransaction().add(android.R.id.content, new MyPreferenceFragment()).commit(); 
    getFragmentManager().beginTransaction().add(android.R.id.content, new MyPreferenceFragment()).commit(); 


} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.fragment_settings, container, false); 
} 


public static class MyPreferenceFragment extends PreferenceFragment { 
    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 

    } 
} 

在這個計算器嗎更有做什麼,我想實現的,不同之處在於它的setContentView不是在工作片段How to add a button to PreferenceScreen

這裏是我的preference.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
<EditTextPreference 
    android:key="username" 
    android:summary="Please provide your username" 
    android:title="Your Name"> 

</EditTextPreference> 
<CheckBoxPreference 
    android:defaultValue="false" 
    android:key="applicationUpdates" 
    android:summary="This option if selected will allow the application to check for latest versions." 
    android:title="Application Updates" /> 
<ListPreference 
    android:defaultValue="1" 
    android:entries="@array/listArray" 
    android:entryValues="@array/listValues" 
    android:key="downloadType" 
    android:summary="Select the kind of data that you would like to download" 
    android:title="Download Details" /> 

我的片段XML

<FrameLayout 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" 
tools:context="nl.shacklez.mijnreceptenofficialv1.SettingsFragment"> 

<!-- TODO: Update blank fragment layout --> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

回答

0

這可能會解決你的問題

添加Fragment嘗試更換Fragment

喜歡更換

getFragmentManager().beginTransaction().add(android.R.id.content, new MyPreferenceFragment()).commit(); 

通過

getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit(); 
+0

不會改變任何東西,它一開始是替換,但後來我意識到了preferences.xml,沒有佈局,所以我希望添加我的片段的佈局。像添加2個片段或其他東西。但我不知道如何去實現這一點 –

0

要設置片段作爲視圖的兩倍。您在onCreateView設置它 - 它實例化一個新的preferenceFragment

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
// Inflate the layout for this fragment 
return inflater.inflate(R.layout.fragment_settings, container, false); 
} 

,然後你調用

getFragmentManager().beginTransaction().add(android.R.id.content, new MyPreferenceFragment()).commit(); 

再次調用你preferenceFragment的OnCreate所以你兩次結合的片段。