TabLayout的基本使用

TabLayout

首先供上Google的源码文档https://developer.android.google.cn/reference/android/support/design/widget/TabLayout.html

先放一个例子,这里是用xml配置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff000000"
        app:tabIndicatorColor="#fffffff"
        app:tabIndicatorHeight="10dp"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="#ffffffff"
        app:tabTextColor="#99ffffff"
        >

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个Item"
            />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二个Item"
            />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@mipmap/ic_launcher"/>

    </android.support.design.widget.TabLayout>
</LinearLayout>

首先是xml的属性设置

常用的有:
tabMode 有两个供选择
fixed:所有的item全部挤在一起,即所有的item都会在界面中显示出来
scrollable:item按照他们自己的长度来显示,如果总长度超过了界面长度,则可进行滚动(常用的就是这个吧)

item的长度按照下面的两个属性进行限制
tabMaxWidth 每个item的最大长度
tabMinWidth 每个item的最小长度

tabIndicatorColor 设置指示器的颜色,也就是选中item后在下方会有一个横线指示器,可以对其进行设置
tabIndicatorHeight 设置指示器的高度

tabTextColor 所有item的文字的颜色
tabSelectedTextColor 选中的item的文字的颜色(覆盖tabTextColor)

TabItem 用这个来放item,当然也可以在java代码里创建

接下来在看用java代码怎样创建item


public class TestActivity  extends AppCompatActivity{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.tablayout_test);
        TabLayout tabLayout= (TabLayout) findViewById (R.id.tablayout_test);
        tabLayout.addTab (tabLayout.newTab ().setText ("这是第三个Item"));
        tabLayout.addTab (tabLayout.newTab ().setText ("这是第四个Item"));
        tabLayout.addTab (tabLayout.newTab ().setText ("这是第五个Item"));
        tabLayout.addTab (tabLayout.newTab ().setIcon (R.mipmap.ic_launcher));
    }
}

没错-。-就这么简单,一句代码就ok

然后就是对每个item的点击事件操作

 tabLayout.addOnTabSelectedListener (new TabLayout.OnTabSelectedListener () {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                 Toast.makeText (TestActivity.this,tab.getText ().toString ()+"",Toast.LENGTH_SHORT).show ();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

OK,这样,整个TabLayout就已经做好了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值