[英]PreferenceFragment overlapping on Fragment layout
I have been able to implement a settings page using a PreferenceFragment. 我已经能够使用PreferenceFragment实现设置页面。 This displays just fine until I navigate to another page.
在我导航到另一页之前,这显示得很好。 The settings page is still showing on top of other pages and I don't know how to replace it completely.
设置页面仍显示在其他页面的顶部,我不知道如何完全替换它。
This image is an example of what I mean. 此图像是我的意思的示例。 I have navigated from the settings page to the home page via the navigation drawer.
我已经通过导航抽屉从设置页面导航到主页。 I think I am missing something from the
replace()
method but I don't know what. 我认为我在
replace()
方法中缺少了一些东西,但我不知道是什么。
AccountSettings.java 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 (nav drawer) MainPage.java (导航抽屉)
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
if (position == 0) {
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(1))
.commit();
} else if (position == 1) {
fragmentManager.beginTransaction()
.replace(R.id.container, Search.newInstance(2))
.commit();
}
else if (position == 2) {
fragmentManager.beginTransaction()
.replace(R.id.container, Favourites.newInstance(3))
.commit();
}
else if (position == 3) {
fragmentManager.beginTransaction()
.replace(R.id.container, History.newInstance(4))
.commit();
}
else if (position == 4) {
getFragmentManager().beginTransaction()
.replace(R.id.container, AccountSettings.newInstance(5))
.commit();
}
}
settings.xml 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>
activity_main_page.xml activity_main_page.xml
<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"
/>
Please help! 请帮忙!
In the fragments' oncreateView
method add this line 在片段的
oncreateView
方法中添加此行
container.removeAllViews()
before inflating the view of the fragment. 在放大片段视图之前。 This helped me in this problem
这帮助我解决了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.