英文:
SettingsActivity like Google Chrome (sub menu with radio buttons)
问题
我正在尝试重新创建谷歌 Chrome 的设置菜单。它应该看起来像下面的图片:
我目前已经有的东西:
- MainActivity.java
- SettingsActivity.java
- SettingsFragment.java
- SettingsSearchEngineFragment.java
- preferences.xml
- preferences_searchengine.xml
我似乎找不到任何关于 androidx 的解释或教程。我可以从 MainActivity 转到 SettingsActivity,但从那里我找不到一种方法来打开我的 "Search Engine" 片段(?)。
我如何实现它?
英文:
I am trying to recreate the settings menu from google chrome.
It should look like the image below:
What I have so far:
- MainActivity.java
- SettingsActivity.java
- SettingsFragment.java
- SettingsSearchEngineFragment.java
- preferences.xml
- preferences_searchengine.xml
I can't seem to find any explanations, tutorials for androidx.
I can go from the MainActivity to the SettingsActivity, but from there out I can't find a way to open my "Search Engine" Fragment(?).
How can I realize it?
答案1
得分: 2
从Android文档:
要管理您的活动中的片段,您需要使用FragmentManager。要获取它,请从您的活动中调用getSupportFragmentManager()。
在您的活动中使用片段的一个很棒的特性是能够根据用户交互添加、删除、替换和执行其他操作。您提交给活动的每组更改都称为事务,您可以使用FragmentTransaction中的API执行事务。
// 创建新的片段和事务
Fragment searchEngineFragment = new SettingsSearchEngineFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// 用此片段替换fragment_container视图中的内容,
// 并将事务添加到后退栈
transaction.replace(R.id.fragment_container, searchEngineFragment);
transaction.addToBackStack(null);
// 提交事务
transaction.commit();
尽管这是一个可行的解决方案,但有一种更轻松的方法。那就是使用Navigation组件,它与Android Studio非常配合。您最好多做一些研究,因为它将有助于澄清您在Android中使用片段、活动甚至对话框进行导航时遇到的任何问题。
链接:https://developer.android.com/guide/navigation/navigation-getting-started
英文:
From the Android Docs:
> 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();
Even though this is a viable solution, there's an approach that is much more stress-free. That is, using the Navigation Components, that work very well with Android Studio.
You'd do well to research more on it as it'd help clarify any issues you have with Navigation in Android both with Fragments, Activities and even Dialogs.
Link: https://developer.android.com/guide/navigation/navigation-getting-started
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论