在Android Studio中,如何使用java代码将片段切换到另一个片段

时间:2016-08-13 16:55:54

标签: java android android-fragments

activity_main上我有btnTogglefragment_textenter image description here

我想在用户点按fragment_text时将fragment_image切换为btnToggleenter image description here

private void btnToggle_OnClick() {
    //what goes here?
}

1 个答案:

答案 0 :(得分:0)

首先动态添加片段然后替换如下: -

FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
SectionDescriptionFragment bdf = new SectionDescriptionFragment();
ft.replace(R.id.book_description_fragment, bdf);
ft.commit();