有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java首选项片段布局上的片段重叠

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

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

enter image description here

帐户设置。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() {

}

}

主页。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();

    }

}

设置。xml

<PreferenceScreen xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:key="prefs">

<PreferenceCategory 安卓:title="@string/pref_search_category" >
<CheckBoxPreference
    安卓:defaultValue="false"
    安卓:key="prefDateTime"
    安卓:summary="@string/pref_night_day_description"
    安卓:title="@string/pref_night_day" >
</CheckBoxPreference>
</PreferenceCategory>

</PreferenceScreen>

活动主页。xml

<安卓.support.v4.widget.DrawerLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    安卓:id="@+id/drawer_layout"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".MainPage">

<FrameLayout 安卓:id="@+id/container" 安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".AccountSettings"
    />

<fragment 安卓:id="@+id/navigation_drawer"
    安卓:layout_width="@dimen/navigation_drawer_width" 安卓:layout_height="match_parent"
    安卓:layout_gravity="start"
    安卓:name="com.example.laptop.whatsfordinner.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer"
    />

请帮忙


共 (1) 个答案

  1. # 1 楼答案

    在fragments'oncreateView方法中添加这一行

    container.removeAllViews()
    

    在膨胀碎片的视图之前。这对我有帮助