gpt4 book ai didi

java - PreferenceFragment 在 Fragment 布局上重叠

转载 作者:行者123 更新时间:2023-12-02 03:48:05 26 4
gpt4 key购买 nike

我已经能够使用 PreferenceFragment 实现设置页面。在我导航到另一个页面之前,它显示得很好。设置页面仍然显示在其他页面的顶部,我不知道如何完全替换它。

这张图片是我的意思的一个例子。我已通过抽屉导航从设置页面导航到主页。我认为我在 replace() 方法中遗漏了一些东西,但我不知道是什么。

enter image description here

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(抽屉导航)

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

<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

<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"
/>

请帮忙!

最佳答案

在 fragment 的 oncreateView 方法中添加此行

container.removeAllViews()

在扩展 fragment 的 View 之前。这对我有帮助this problem

关于java - PreferenceFragment 在 Fragment 布局上重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36147254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com