有 Java 编程相关的问题?

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

java设置类似Google Chrome的活动(带有单选按钮的子菜单)

我正在尝试从谷歌浏览器重新创建设置菜单。 它应该如下图所示: enter image description here

到目前为止,我所拥有的:

  1. 主要活动。爪哇
  2. 设置活动性。爪哇
  3. 设置碎片。爪哇
  4. 设置搜索引擎片段。爪哇
  5. 偏好。xml
  6. 首选项搜索引擎。xml

我似乎找不到安卓x的任何解释和教程。 我可以从Main活动转到SettingsActivity,但从那里我找不到打开“搜索引擎”片段(?)的方法

我怎样才能实现它


共 (1) 个答案

  1. # 1 楼答案

    从Android文档:

    To manage the fragments in your activity, you need to use FragmentManager. To get it, call getSupportFragmentManager() from your activity.

    A great feature about using fragments in your activity is the ability to add, remove, replace, and perform other actions with them, in response to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in FragmentTransaction

    // Create new fragment and transaction
    Fragment searchEngineFragment = new SettingsSearchEngineFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    
    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack
    transaction.replace(R.id.fragment_container, searchEngineFragment);
    transaction.addToBackStack(null);
    
    // Commit the transaction
    transaction.commit();
    

    尽管这是一个可行的解决方案,但也有一种方法更能消除压力。也就是说,使用导航组件,可以很好地与Android Studio配合使用。 你最好对它进行更多的研究,因为它有助于澄清你在Android中导航的任何问题,包括片段、活动甚至对话框

    链接:https://developer.android.com/guide/navigation/navigation-getting-started