如何使用TabLayout Android Design创建带有图标的应用栏? [英] How to create app bar with icons using TabLayout Android Design?

查看:102
本文介绍了如何使用TabLayout Android Design创建带有图标的应用栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用android设计库中的新TabLayout创建带有图标的应用栏.

I'm trying to use the new TabLayout in the android design library to create app bar with icons.

public void setupTabLayout(TabLayout tabLayout) {
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
    tabLayout.setupWithViewPager(mViewpager);
    tabLayout.getTabAt(0).setIcon(R.drawable.ic_tabbar_library);
    tabLayout.getTabAt(1).setIcon(R.drawable.ic_tabbar_recents);
    tabLayout.getTabAt(2).setIcon(R.drawable.ic_tabbar_favorites);
    tabLayout.getTabAt(3).setIcon(R.drawable.ic_tabbar_notifications);
    tabLayout.getTabAt(4).setIcon(R.drawable.ic_tabbar_settings);
}

结果:

请帮助我创建类似以下的应用栏:

Please help me to create app bar similar:

对不起,我的英语不好.谢谢前进!

Sorry my english is not good.Thanks is advance !

推荐答案

来自文档:

https://developer.android.com/reference/android/support/design/widget/TabLayout.Tab.html#setCustomView(android.view.View)

设置用于此选项卡的自定义视图.这将覆盖设置的值 通过setText(CharSequence)和setIcon(Drawable).

Set a custom view to be used for this tab. This overrides values set by setText(CharSequence) and setIcon(Drawable).

您将必须自行设置标题值

根据您的示例:

public void setupTabLayout(TabLayout tabLayout) {
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
    tabLayout.setupWithViewPager(mViewpager);

    TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tab.setText("Library");
    tab.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tabbar_library, 0, 0);
    tabLayout.getTabAt(0).setCustomView(tab);
    //..
}

custom_tab.xml

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tab" />

更新

API已更改为允许您设置自定义ID,因此您不必手动设置文本/可绘制对象.它将使用适配器的值.

The api has changed to allow you to set a custom id so you don't have to set the text/drawable manually. It'll use the adapter's values.

如果提供的视图包含ID为text1的TextView,则该 将使用给定setText(CharSequence)的值进行更新. 同样,如果此布局包含带有ID图标的ImageView,则它 将使用给定的setIcon(Drawable)值进行更新.

If the provided view contains a TextView with an ID of text1 then that will be updated with the value given to setText(CharSequence). Similarly, if this layout contains an ImageView with ID icon then it will be updated with the value given to setIcon(Drawable).

这篇关于如何使用TabLayout Android Design创建带有图标的应用栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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