Android L 中不推荐使用操作栏导航模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24473213/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:15:11  来源:igfitidea点击:

Action bar navigation modes are deprecated in Android L

androidandroid-actionbarandroid-navigationandroid-5.0-lollipop

提问by matiash

Taking a look at the API diff report for the Android "L" preview, I see that all methods related to navigation modes in the ActionBarclass (such as setNavigationMode(), addTab(), selectTab(), &c). are now deprecated.

纵观在为Android“L”预览API diff报告,我看到与在导航模式的所有方法ActionBar类(如setNavigationMode()addTab()selectTab(),&C)。现在已弃用。

The documentation explains:

文档解释了:

Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead.

内联工具栏操作栏已弃用且不支持操作栏导航模式。考虑使用其他常见的导航模式。

What is the supposed replacement?

什么是所谓的替代?

Also, is "inline toolbar action bars" a new concept? I don't think I've heard of it before.

另外,“内联工具栏操作栏”是一个新概念吗?我想我以前没有听说过。

采纳答案by ianhanniballake

The new Android Design Support Libraryadds TabLayout, providing a tab implementation that matches the material design guidelines for tabs. A complete walkthrough of how to implement Tabs and ViewPagercan be found in this video

新的Android 设计支持库添加了TabLayout,提供与标签的材料设计指南相匹配的标签实现。有关如何实现 Tabs 的完整演练,ViewPager可在此视频中找到

Now deprecated:The PagerTabStripis part of the support library (and has been for some time) and serves as a direct replacement. If you prefer the newer Google Play style tabs, you can use the PagerSlidingTabStrip libraryor modify either of the Google provided examples SlidingTabsBasicor SlidingTabsColorsas explained in this Dev Bytes video.

现在弃用:PagerTabStrip是支持库的一部分(已经有一段时间),并作为直接替换。如果您更喜欢较新的 Google Play 样式选项卡,您可以使用PagerSlidingTabStrip 库或修改 Google 提供的示例SlidingTabsBasicSlidingTabsColors 中的任一个,如此Dev Bytes 视频 中所述

回答by matiash

Now that the Android 5.0 docs are available, we have the official documentation for the Toolbar widget:

现在 Android 5.0 文档可用,我们有工具栏小部件官方文档

A standard toolbar for use within application content.

A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy.

在应用程序内容中使用的标准工具栏。

工具栏是在应用程序布局中使用的操作栏的概括。虽然操作栏传统上是由框架控制的活动不透明窗口装饰的一部分,但工具栏可以放置在视图层次结构中的任意嵌套级别

A Toolbarwidget can also be used to replace the action bar:

一个Toolbar小工具也可以用来代替行动吧:

An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar()method.

应用程序可以选择将工具栏指定为使用该setActionBar()方法的活动的操作栏。

The deprecation of tabs in the action bar is most probably due to this, since toolbars cannot contain tab themselves.

操作栏中标签的弃用很可能是由于这个原因,因为工具栏不能包含标签本身。

Also, it's available for previous Android verions via the appcompat library. See this post by Chris Banesfor more information. An excerpt:

此外,它可通过 appcompat 库用于以前的 Android 版本。有关更多信息,请参阅Chris Banes 的这篇文章。摘录:

Android 5.0 introduces a new Toolbar widget. This is a generalization of the ActionBar pattern but gives you muchmore control and flexibility in using it. Toolbar is a view in your hierarchy just like any other, making it easier to interleave with the rest of your views, animate, react to scroll events.

Android 5.0 引入了一个新的工具栏小部件。这是 ActionBar 模式的概括,但在使用它时为您提供更多的控制和灵活性。工具栏是您层次结构中的一个视图,就像任何其他视图一样,可以更轻松地与其余视图交错、动画、对滚动事件做出反应。

回答by Kuno

It seems like they added a new Class named android.widget.Toolbarthat extends ViewGroup. Also they added a new method setActionBar(Toolbar)in Activity. I haven't tested it yet, but it looks like you can wrap all kinds of TabWidgets, Spinnersor custom views into a Toolbarand use it as your Actionbar.

似乎他们添加了一个名为android.widget.Toolbarextends的新类ViewGroup。他们setActionBar(Toolbar)还在Activity. 我没有测试过,但它看起来像你可以用各种各样的TabWidgetsSpinners或者自定义视图进入Toolbar并使用它作为你的动作条。

回答by NeoKree

The new Toolbarcannot be used for inflating multiple line objects, so it is impossible to add Tabs to it.
If you want to use a Toolbarlike a TabWidgetyou can insert some Tab Objects to it, but only with the old Holo style.

newToolbar不能用于膨胀多个线对象,因此无法为其添加 Tabs。
如果您想使用Toolbar类似 a,TabWidget您可以向其中插入一些 Tab 对象,但只能使用旧的 Holo 样式。

Herethere is a custom Library that uses v7 Toolbarlike TabWidgetwith the new Material Design animations, but it uses the same methods from the old ActionBar Tabs, so you can attach your ViewPagerto it.

这里有一个自定义库,它ToolbarTabWidget新的 Material Design 动画一样使用 v7 ,但它使用与旧的相同的方法ActionBar Tabs,因此您可以将ViewPager其附加到它。

回答by applesauced

For 'replacement' of deprecated ActionBar, I changed the type of my ActionBar-type variables to PagerTabStrip, as per (old code in comment):

为了“替换”已弃用的 ActionBar,我根据(注释中的旧代码)将 ActionBar 类型变量的类型更改为 PagerTabStrip:

// ActionBar bigActionBar;
PagerTabStrip bigActionBar;

A 'replacement' for ~actionBar's .selectTab(tabindex) was to use my associated ViewPager's .setCurrentItem(int) method, like this (old code in comment):

~actionBar 的 .selectTab(tabindex) 的“替换”是使用我关联的 ViewPager 的 .setCurrentItem(int) 方法,如下所示(注释中的旧代码):

/*
ActionBar.Tab eventTab = bigActionBar.getTabAt(2);
bigActionBar.selectTab(eventTab);
*/
mViewPager.setCurrentItem(2);

Hope this is helpful.

希望这是有帮助的。

回答by AR Ross

I had the same problem and this solution suited me quite nicely:

我遇到了同样的问题,这个解决方案非常适合我:

In the layout xml file that contains the viewpager, add the a PagerTabStrip as shown:

在包含 viewpager 的布局 xml 文件中,添加一个 PagerTabStrip,如下所示:

<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_tab_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#996633"
    android:textColor="#CCCCCC"
    android:paddingTop="5dp"
    android:paddingBottom="5dp" />

To control page titles, add a switch statement to your ViewPager file:

要控制页面标题,请在 ViewPager 文件中添加 switch 语句:

@Override
public CharSequence getPageTitle(int position)
{
    switch (position)
    {
    case 0:
        return "Page 1";

    case 1:
        return "Page 2";

    case 2:
        return "Page 3";

    }

    return null;
}

回答by Anuj

FragmentTabHost is also an option.

FragmentTabHost 也是一个选项。

This code is from Android developer's site:

此代码来自 Android 开发人员的站点:

/**
 * This demonstrates how you can implement switching between the tabs of a
 * TabHost through fragments, using FragmentTabHost.
 */
public class FragmentTabs extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_tabs);
    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
            FragmentStackSupport.CountingFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
            LoaderCursorSupport.CursorLoaderListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
            LoaderCustomSupport.AppListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
            LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
    }
}

回答by kip2

I found these tutorials helpful while putting together an action bar (now the 'tool bar' - argh) that supports sliding tabs with Material Design:

我发现这些教程在组合使用 Material Design 支持滑动选项卡的操作栏(现在是“工具栏” - argh)时很有帮助:

https://www.youtube.com/watch?v=Fl0xMuo10yA

https://www.youtube.com/watch?v=Fl0xMuo10yA

http://www.exoguru.com/android/material-design/navigation/android-sliding-tabs-with-material-design.html

http://www.exoguru.com/android/material-design/navigation/android-sliding-tabs-with-material-design.html

You sort of have to synthesize these resources to match your particular situation. For example, you may not want to manually create the tabs in the same style that the exoguru.com tutorial did.

您必须综合这些资源以匹配您的特定情况。例如,您可能不想以与 exoguru.com 教程相同的样式手动创建选项卡。

回答by Newbie Developer

Well for me to handle the deprecated navigation toolbar by using toolbar v7 widget appcompat.

对我来说,通过使用工具栏 v7 小部件 appcompat 来处理已弃用的导航工具栏。

    setSupportActionBar(toolbar);
    getSupportActionBar().setSubtitle("Feed Detail");
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //goToWhere
        }
    });

回答by Andrew S

I think a suitable replacement for when you have three to five screens of equal importance is the BottomNavigationActivity,this can be used to switch fragments.

我认为当你有三到五个同等重要的屏幕时,一个合适的替代品是 BottomNavigationActivity,它可以用来切换片段。

You will notice a wizard exists for this in Android Studio, take care however as Android Studio has a tendency to produce overly complex boiler plate code.

您会注意到 Android Studio 中存在一个用于此的向导,但请注意,因为 Android Studio 倾向于生成过于复杂的样板代码。

A tutorial can be found here: https://android.jlelse.eu/ultimate-guide-to-bottom-navigation-on-android-75e4efb8105f

可以在此处找到教程:https: //android.jlelse.eu/ultimate-guide-to-bottom-navigation-on-android-75e4efb8105f

Another quality tutorial can be found at Android Hive here: https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/

另一个质量教程可以在 Android Hive 上找到:https: //www.androidhive.info/2017/12/android-working-with-bottom-navigation/