使用Tab与新ToolBar(AppCompat v7-21)

问题描述 投票:69回答:2

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

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

我从ActionBar迁移到工具栏。我的应用程序确实需要支持API 9。

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

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

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

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

一些帮助?

android tabs android-support-library android-appcompat android-actionbar-compat
2个回答
156
投票

使用API​​ 21,方法setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)deprecated

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

它使用SlidingTabLayoutViewPager一起使用。

Here you can find the example(它在你的sdk例子中)

在这里您可以找到Google io14示例:

更新29/05/2015

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

只需将此依赖项添加到build.gradle即可

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

代码很简单:

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

要实现材料设计的许多功能,您应该在CoordinatorLayoutAppBarLayout中使用它。

像这样的东西:

 <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>

14
投票

虽然现在回答这个问题可能有点晚了,但我意识到我编写了an answer on a similar question,其中包括使用设计支持库和Google I / O之前的版本。

我已经包括以下基本部分:


自Android设计支持库发布以来,使用带有TabLayoutToolbar变得更加简单,这意味着我们不再需要下载自定义视图类。

来自Android Developers' Blogspot post on the Android Design Support Library

标签:

通过tabs在您的应用程序中的不同视图之间切换并不是材料设计的新概念,它们在家中作为top level navigation pattern或在您的应用程序中组织不同的内容分组(例如,不同类型的音乐)。

设计库的TabLayout实现了两个固定选项卡,其中视图的宽度在所有选项卡之间平均分配,以及可滚动选项卡,其中选项卡不是统一大小并且可以水平滚动。标签可以通过编程方式添加:

TabLayout tabLayout = ...; tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));

但是,如果您使用ViewPager在标签之间进行水平分页,则可以直接从PagerAdaptergetPageTitle()创建标签,然后使用setupWithViewPager()将两者连接在一起。这可确保选项卡选择事件更新ViewPager,页面更改将更新选定的选项卡。


如果您不使用设计支持库,则需要执行以下操作:

1.从GitHub上的Google I / O Conference应用程序下载SlidingTabLayout.javaSlidingTabStrip.java文件。这些将是在选项卡布局中使用的视图,因此我创建了一个包含其他Java活动的文件夹,名为“view”并将它们放在那里。

2.编辑您的布局以包含SlidingTabLayout

<LinearLayout
    android:orientation="vertical"
    ... >

    <!-- This is the Toolbar with the tabs underneath -->
    <LinearLayout
        android:orientation="vertical"
        ... >

        <include android:id="@+id/my_toolbar" layout="@layout/toolbar" />

        <com.mycompany.myapp.SlidingTabLayout
            android:id="@+id/sliding_tabs"
            android:background="?attr/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <!-- This is the ViewPager (which you already should have if you have
        used tabs before) -->
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    ...

</LinearLayout>

引用Toolbar<include android:id="@+id/detail_toolbar" layout="@layout/toolbar" />)的行引用了我用来制作Toolbar的另一个XML文件。

3.更改SlidingTabLayout.javaSlidingTabStrip.java中与其放置位置对应的包名称。就我而言,我使用类似于:package com.mycompany.myapp.view;的两个文件。如您正在使用的IDE所述,组织导入并添加任何必要的内容。

4.在你的Activity(扩展AppCompatActivity)中,在Toolbar方法中设置onCreate

Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);

5.设置ViewPagerSlidingTabLayout部件:

mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.tab_line));   
mSlidingTabLayout.setDistributeEvenly(true);
mSlidingTabLayout.setViewPager(mViewPager);

颜色'tab_line'是我在color.xml声明的颜色,它将是标签线指示器的颜色。另请注意,上面的变量是我在此活动中先前定义的全局变量:

SlidingTabLayout mSlidingTabLayout;
ViewPager mViewPager;

6.最后,设置我之前调用过的ViewPagerAdapter。这将负责根据选择的选项卡更改页面。我使用了以下代码:

public class ViewPagerAdapter extends FragmentPagerAdapter {

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        // Returns the number of tabs
        return 3;
    }

    @Override
    public Fragment getItem(int position) {
        // Returns a new instance of the fragment
        switch (position) {
            case 0:
                return new FragmentOne();
            case 1:
                return new FragmentTwo();
            case 2:
                return new FragmentThree();
        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

如上所述,这些解决方案的更多细节可以在another question I answered, about using Sliding Tabs with the Toolbar上找到。

© www.soinside.com 2019 - 2024. All rights reserved.