Android:不推荐使用 TabActivity,使用 Fragments?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12382389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: TabActivity deprecated, use Fragments?
提问by Martin
It appears that TabActivity is now deprecated, we need to use Fragments.
现在看来 TabActivity 已被弃用,我们需要使用 Fragments。
I tried using a standard Activity but couldn't call getTabHost.
我尝试使用标准活动,但无法调用 getTabHost。
So it appears i have to use Fragments. But i am a little confused how this would work.
所以看来我必须使用片段。但我有点困惑这将如何工作。
I understand that fragments are not activities so they are not in the manifest file?
我知道片段不是活动,所以它们不在清单文件中?
SO i presume i can't do startActivity on a fragment?
所以我想我不能在片段上做 startActivity 吗?
Does anyone know of a good example explaining the tabHost and Fragments, all the examples and tutorials i have found are only using the tabactivity.
有谁知道一个解释 tabHost 和 Fragments 的好例子,我发现的所有例子和教程都只使用 tabactivity。
Thanks in advance
提前致谢
回答by ??? ???? ????
Maybe you could use TabLayout instead.
也许您可以改用 TabLayout。
Tabs are now best implemented by leveraging the ViewPager with a custom "tab indicator" on top. Google's new TabLayout included in the support design library release for Android "M".
现在最好通过利用顶部带有自定义“选项卡指示器”的 ViewPager 来实现选项卡。Google 的新 TabLayout 包含在 Android“M”的支持设计库版本中。
Visit the full tutorial of using TabLayout in Google Play Style Tabs using TabLayoutEdit PagePage History
访问使用 TabLayoutEdit PagePage History在Google Play Style Tabs 中使用 TabLayout 的完整教程
回答by GensaGames
I think your problem with using selected realization. Android SDK contains a lot of variants of View, such as ActivityList, TabActivity, ActionBarActivity, etc. And they all deprecated or will deprecated. You should ask why?
我认为您使用选定实现的问题。Android SDK 中包含了很多 View 的变种,比如 ActivityList、TabActivity、ActionBarActivity 等,它们都被弃用或将被弃用。你应该问为什么?
- Look at the different example with Tabs and List in new Material. Application contains main header, like ToolbarLayout, and in this contains different state of Toolbar, you may add TabLayout, and other cool things.
- 在新材质中查看带有选项卡和列表的不同示例。应用程序包含主标题,如 ToolbarLayout,在此包含 Toolbar 的不同状态,您可以添加 TabLayout 和其他很酷的东西。
So answer on your question - It's better to use customize and powerful view elements, then several implementations of main action view (Activity). This reason still more visible, when you look to action specification of elements. Activity don't need to take place in view initialization.
所以回答你的问题 - 最好使用自定义和强大的视图元素,然后是主动作视图(活动)的几种实现。当您查看元素的动作规范时,这个原因更加明显。Activity 不需要发生在视图初始化中。
回答by Ilya Gazman
Today Android got a nice tutorials for those. Start at Creating Swipe Views with Tabs
今天Android为这些人提供了一个很好的教程。从使用选项卡创建滑动视图开始
Here is a small snapshot of how to create tabs
这是如何创建选项卡的小快照
@Override
public void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
...
// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
}
回答by Irfan Raza
First - you can startActivity from your fragment by something like context.startActivity(....);
首先 - 您可以通过诸如 context.startActivity(....); 之类的东西从片段中启动活动;
Second to understand the tabs using fragment just create one sample project on your Android Studio and when it asks you to choose the existing template then choose the one which is having tabs. This way you will see the standard code.
其次要了解使用片段的选项卡,只需在您的 Android Studio 上创建一个示例项目,当它要求您选择现有模板时,然后选择具有选项卡的模板。这样,您将看到标准代码。
回答by Sampath
Lets face it. Fragments are the future. And we are being guided towards using them.
面对现实吧。碎片是未来。我们正在被引导使用它们。
I found the following tutorials to be quite informative and hope they would answer all your 'Tabs with Fragments questions':
我发现以下教程内容丰富,希望它们能回答您所有的“带片段的标签问题”: