Java 片段布局上的首选片段重叠
我已经能够使用PreferenceFragment实现设置页面。在我导航到另一个页面之前,它显示得很好。设置页面仍然显示在其他页面的顶部,我不知道如何完全替换它 这张图片就是我的意思的一个例子。我已通过导航抽屉从设置页面导航到主页。我想我在Java 片段布局上的首选片段重叠,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,我已经能够使用PreferenceFragment实现设置页面。在我导航到另一个页面之前,它显示得很好。设置页面仍然显示在其他页面的顶部,我不知道如何完全替换它 这张图片就是我的意思的一个例子。我已通过导航抽屉从设置页面导航到主页。我想我在replace()方法中遗漏了一些东西,但我不知道是什么 AccountSettings.java public class AccountSettings extends PreferenceFragment { Activity mActivity;
replace()
方法中遗漏了一些东西,但我不知道是什么
AccountSettings.java
public class AccountSettings extends PreferenceFragment {
Activity mActivity;
private static final String ARG_SECTION_NUMBER = "section_number";
public static AccountSettings newInstance(int sectionNumber) {
AccountSettings fragment = new AccountSettings();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().setBackgroundColor(Color.WHITE);
getView().setClickable(true);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDetach() {
super.onDetach();
}
public void restoreActionBar() {
ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle("Account Settings");
}
public AccountSettings() {
}
}
MainPage.java(导航抽屉)
settings.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="prefs">
<PreferenceCategory android:title="@string/pref_search_category" >
<CheckBoxPreference
android:defaultValue="false"
android:key="prefDateTime"
android:summary="@string/pref_night_day_description"
android:title="@string/pref_night_day" >
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainPage">
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AccountSettings"
/>
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.example.laptop.whatsfordinner.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer"
/>
活动\u主页.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="prefs">
<PreferenceCategory android:title="@string/pref_search_category" >
<CheckBoxPreference
android:defaultValue="false"
android:key="prefDateTime"
android:summary="@string/pref_night_day_description"
android:title="@string/pref_night_day" >
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainPage">
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AccountSettings"
/>
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.example.laptop.whatsfordinner.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer"
/>
请帮忙 在片段'
oncreateView
方法中添加此行
container.removeAllViews()
在膨胀片段视图之前。这有助于我在片段的oncreateView方法中添加这一行
container.removeAllViews()
在膨胀片段视图之前。这帮了我的忙