我是Android开发的新手。我创建了一个简单的主要活动,并在左上角添加了一个图标。单击它我可以在我的屏幕上显示一个空白片段,替换在onCreate方法中加载的布局。现在单击另一个图标我想隐藏该片段并再次加载该布局。怎么做??任何帮助? 以下是我的代码
//part of oncreate where my layout is loaded
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// part of code when icon clicked and fragment is loaded
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment frag = new BlankFragment();
fragmentTransaction.replace(R.id.content_main, frag);
fragmentTransaction.commit();
//another nearby icon clicked
//now i want to replace this fragment from content_main layout
//what code to add??
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以通过以下代码切换visibilty。
public void toggleVisibility(Fragment fragment){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (fragment.isHidden()) {
transaction.show(fragment);
} else {
transaction.hide(fragment);
}
transaction.commit();
}