XTabLayout的使用



http://blog.csdn.net/a1533588867/article/details/53810409


添加依赖


compile 'com.androidkun:XTabLayout:1.1.3'


xml


 <com.androidkun.xtablayout.XTabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:xTabDisplayNum="3"
        app:xTabBackgroundColor="#abb"
        app:xTabSelectedBackgroundColor="#baa"
        app:xTabDividerColor="#ff0"
        app:xTabDividerGravity="center"
        app:xTabDividerHeight="10dp"
        app:xTabDividerWidth="2dp"
        app:xTabIndicatorColor="#0f0"
        app:xTabIndicatorHeight="3dp"
        app:xTabIndicatorWidth="10dp"
        app:xTabMode="scrollable"
        app:xTabSelectedTextColor="#0f0"
        app:xTabSelectedTextSize="25sp"
        app:xTabTextColor="#0ff"
        app:xTabTextSize="20sp" />


 <com.androidkun.xtablayout.XTabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"


        app:xTabDisplayNum="3"  // 屏幕中显示的tab个数


        app:xTabBackgroundColor="#abb"
        app:xTabSelectedBackgroundColor="#baa"  // tab的背景色


        app:xTabDividerColor="#ff0"
        app:xTabDividerGravity="center"
        app:xTabDividerHeight="10dp"
        app:xTabDividerWidth="2dp"  // 设置分隔线,分隔线最好不要与背景色混用,容易出现显示的混乱


        app:xTabIndicatorColor="#0f0"
        app:xTabIndicatorHeight="3dp"
        app:xTabIndicatorWidth="10dp" // 设置指示器


        app:xTabMode="scrollable"


        app:xTabSelectedTextColor="#0f0"
        app:xTabSelectedTextSize="25sp"
        app:xTabTextColor="#0ff"
        app:xTabTextSize="20sp" // 设置字体的颜色、大小


/>




java




package chad.com.shapplication.test;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import com.androidkun.xtablayout.XTabLayout;
import java.util.ArrayList;
import java.util.List;
import chad.com.shapplication.R;
import chad.com.shapplication.activity.BaseActivity;


public class TestActivity extends BaseActivity {


    private XTabLayout mTabLayout = null;
    private ViewPager mViewPager = null;
    List<Fragment> fragments = null;
    String[] titles = null;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        mTabLayout = (XTabLayout) findViewById(R.id.tab_layout);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);


        // 添加tab
//        mTabLayout.addTab(mTabLayout.newTab().setText("1"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("12"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("123"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("1234"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("12345"));


        // 注意,使用选中项与不选中项不同的背景色时,必须设置一下选中哪个,不然一开始时不选中项没有背景色
//        mTabLayout.getTabAt(1).select();


        // 这样可以自定义tab的布局与内容了
//        mTabLayout.getTabAt(0).setCustomView(R.layout.tab_custom_view);
//        mTabLayout.getTabAt(0).getCustomView();


        // 设置监听
        mTabLayout.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                String text = (String) tab.getText();
                toast("选中" + text);
            }


            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {
                String text = (String) tab.getText();
                toast("不要" + text + "了");
            }


            @Override
            public void onTabReselected(XTabLayout.Tab tab) {
                String text = (String) tab.getText();
                toast("再次点击" + text);
            }
        });


        // 与viewPager配合使用


        fragments = new ArrayList<>();
        fragments.add(new TestFragment());
        fragments.add(new TestFragment());
        fragments.add(new TestFragment());
        fragments.add(new TestFragment());
        fragments.add(new TestFragment());


        titles = new String[]{"1", "12", "123", "1234", "12345"};


        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }


            @Override
            public int getCount() {
                return fragments.size();
            }


            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        };


        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);




    }


}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值