用料设计工具栏滑动标签 [英] Sliding Tabs in Toolbar using Material Design

查看:147
本文介绍了用料设计工具栏滑动标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习使用滑动使用材料设计使用<一个标签href="http://www.exoguru.com/android/material-design/navigation/android-sliding-tabs-with-material-design.html"相对=nofollow>这个帖子。我已成功地实现SlidingTabs 下面工具栏,像这样的:

I have been learning to use Sliding Tabs using Material Design using this post. I have managed to achieve SlidingTabs below the Toolbar, like this one:

但现在我想创建动作条/工具栏片段标签

推荐答案

我能够重新正是你在找什么来实现。我现在用的这个的标签。

I was able to recreate exactly what you are looking to implement. I am using the this Library for the tabs.

这是我创建的视图:

导入库通过依赖或下载项目和导入手动

compile 'com.jpardogo.materialtabstrip:library:1.0.9'

styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
    </style>
</resources>

MainActivity和放大器;适配器

public class MainActivity extends ActionBarActivity {
    Toolbar toolbar;
    ViewPager viewPager;
    ContactPagerAdapter pagerAdapter;
    PagerSlidingTabStrip pagerSlidingTabStrip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setLogo(R.mipmap.logo_two);
        toolbar.inflateMenu(R.menu.menu_main);
        viewPager = (ViewPager) findViewById(R.id.pager);
        pagerAdapter = new ContactPagerAdapter(this, getSupportFragmentManager());
        pagerSlidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        viewPager.setAdapter(pagerAdapter);
        pagerSlidingTabStrip.setViewPager(viewPager);
    }

    public static class ContactPagerAdapter extends FragmentPagerAdapter implements PagerSlidingTabStrip.CustomTabProvider {

        private final int[] ICONS = {R.mipmap.ic_launcher, R.mipmap.ic_launcher};
        Context mContext;
        private Fragment f = null;

        public ContactPagerAdapter(Context ctx, FragmentManager fm) {
            super(fm);
            mContext = ctx;
        }

        @Override
        public int getCount() {
            return ICONS.length;
        }

        @Override
        public Fragment getItem(int position) { // Returns Fragment based on position
            switch (position) {
                case 0:
                    f = new FragmentPageOne();
                    break;
                case 1:
                    f = new FragmentPageTwo();
                    break;
            }
            return f;
        }

        @Override
        public View getCustomTabView(ViewGroup parent, int position) {
            LinearLayout customLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.custom_tab, parent, false);
            ImageView imageView = (ImageView) customLayout.findViewById(R.id.image);
            imageView.setImageResource(ICONS[position]);
            return customLayout;
        }
    }
}

activity_main.xml

<RelativeLayout
    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.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#02a6d8"
        android:minHeight="56dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="56dp"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="8dp"
            android:background="#02a6d8"
            app:pstsDividerColor="#02a6d8"
            app:pstsIndicatorColor="#fff"
            app:pstsIndicatorHeight="2dp"
            app:pstsShouldExpand="false"
            app:pstsUnderlineHeight="0dp"/>
    </android.support.v7.widget.Toolbar>


    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"/>

    <!-- Shadow below toolbar-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_below="@+id/toolbar"
        android:background="@drawable/toolbar_shadow"/>
</RelativeLayout>

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="30dp"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"/>
</LinearLayout>

Drawrable toolbar_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="270"
        android:endColor="@android:color/transparent"
        android:startColor="@color/semi_transparent"/>
</shape>

这篇关于用料设计工具栏滑动标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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