SettingsActivity类似于Google Chrome(带有单选按钮的子菜单)

huangapple go评论40阅读模式
英文:

SettingsActivity like Google Chrome (sub menu with radio buttons)

问题

我正在尝试重新创建谷歌 Chrome 的设置菜单。它应该看起来像下面的图片:

SettingsActivity类似于Google Chrome(带有单选按钮的子菜单)

我目前已经有的东西:

  1. MainActivity.java
  2. SettingsActivity.java
  3. SettingsFragment.java
  4. SettingsSearchEngineFragment.java
  5. preferences.xml
  6. 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:
SettingsActivity类似于Google Chrome(带有单选按钮的子菜单)

What I have so far:

  1. MainActivity.java
  2. SettingsActivity.java
  3. SettingsFragment.java
  4. SettingsSearchEngineFragment.java
  5. preferences.xml
  6. 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

huangapple
  • 本文由 发表于 2020年7月29日 18:08:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63151212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定