使用带有新工具栏的 Tab (AppCompat v7-21) [英] Use Tab with new ToolBar (AppCompat v7-21)

查看:31
本文介绍了使用带有新工具栏的 Tab (AppCompat v7-21)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了带有选项卡和自定义 ActionBar 主题的 SupportActionBar(使用 http://jgilfelt 创建.github.io/android-actionbarstylegenerator/),仅在用户展开搜索视图时显示选项卡.

I was using SupportActionBar with tabs and a custom ActionBar theme (created with http://jgilfelt.github.io/android-actionbarstylegenerator/), that show the tabs only when the user expands the search view.

public boolean onMenuItemActionExpand(MenuItem item) {
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        return true;
    }
}

我从 ActionBar 迁移到 Toolbar.我的应用确实需要支持 API 9.

I migrated from ActionBar to Toolbar. My app really needs to support API 9.

有没有办法使用此代码将标签添加回来?:

Is there a way to use this code to add the tabs back?:

Toolbar toolbar = (Toolbar) findViewById(R.id.new_actionbar);
setSupportActionBar(toolbar);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

如果可能,我如何使用我的自定义主题或工具栏样式?

If possible, how can I use my custom theme or style the toolbar?

文档说这已被弃用并建议使用不同类型的导航.但我不知道 Android 中还有哪些其他组件具有相同的功能.

The documentation says that this is deprecated and suggests using a different type of navigation. But I don't know of any other components in Android that have the same functionality.

需要帮助吗?

推荐答案

使用 API 21,方法 setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)已弃用.

With the API 21 the method setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) is deprecated.

更新 01/08/2019(材料组件库)

将依赖项添加到您的 build.gradle:

Add the dependency to your build.gradle:

dependencies { implementation ‘com.google.android.material:material:1.1.0’ }

然后就可以使用新的TabLayout.

Then you can use the new TabLayout.

<androidx.constraintlayout.widget.ConstraintLayout>

     <com.google.android.material.appbar.AppBarLayout   ...>

        <androidx.appcompat.widget.Toolbar  .../>


        <com.google.android.material.tabs.TabLayout
         ...
         />

     </com.google.android.material.appbar.AppBarLayout>

     <androidx.viewpager.widget.ViewPager 
        android:id="@+id/viewpager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.constraintlayout.widget.ConstraintLayout>

代码很简单:

TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(pager);

<小时>

更新 29/05/2015(支持库)

通过新的设计支持库,您现在可以使用TabLayout.

With the new Design Support Library now you can use the TabLayout.

只需将此依赖项添加到您的 build.gradle

Just add this dependency to your build.gradle

compile 'com.android.support:design:22.2.0'

代码很简单:

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

要实现 Material Design 的许多功能,您应该在一个CoordinatorLayout 和一个 AppBarLayout.

To implement many of the features of material designs you should use it within a CoordinatorLayout and a AppBarLayout.

像这样:

 <android.support.design.widget.CoordinatorLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">


     <android.support.design.widget.AppBarLayout
             android:layout_height="wrap_content"
             android:layout_width="match_parent">

         <android.support.v7.widget.Toolbar
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

         <android.support.design.widget.TabLayout
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

     </android.support.design.widget.AppBarLayout>

     <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

 </android.support.design.widget.CoordinatorLayout>

<小时>

您可以使用不同的模式.例如,您可以使用在 googleio14 中看到的相同示例.

You can use a different pattern. For example you can use the same example that you can see in googleio14.

它使用一个 SlidingTabLayout,它与 ViewPager 一起工作.

It uses a SlidingTabLayout which works with a ViewPager.

在这里你可以找到示例(在您的 sdk 示例中)

Here you can find the example (it is in your sdk example)

您可以在此处找到 Google io14 示例:

Here you can find the Google io14 example:

Java

这篇关于使用带有新工具栏的 Tab (AppCompat v7-21)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆