美文网首页Android开发常用代码
TabLayout自定义tab并实现tab点击切换

TabLayout自定义tab并实现tab点击切换

作者: 才兄说 | 来源:发表于2017-06-09 12:42 被阅读57次

    目录:android.support.design.widget.TabLayout

    xml中:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.TabLayout
            android:id="@+id/toolbar_tl_tab"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_gravity="bottom"
            app:layout_scrollFlags="scroll"
            app:tabIndicatorColor="@android:color/holo_green_light"
            app:tabSelectedTextColor="@android:color/holo_green_light" />
    </RelativeLayout>
    

    代码调用:

    public class MainActivity extends AppCompatActivity {
    
        private TabLayout toolbar_tl_tab;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            toolbar_tl_tab = (TabLayout) findViewById(R.id.toolbar_tl_tab);
            toolbar_tl_tab.setTabMode(TabLayout.MODE_FIXED);
            String[] titles = {"标题1", "标题2", "标题3", "标题4"};
            for (int i = 0; i < titles.length; i++) {
                toolbar_tl_tab.addTab(toolbar_tl_tab.newTab().setText(titles[i]), i);
                //设置tab选中及默认状态
                toolbar_tl_tab.setTabTextColors(getResources().getColor(android.R.color.background_dark), getResources().getColor(android.R.color.holo_red_dark));
            }
            toolbar_tl_tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                //选中是回调
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    switch (tab.getPosition()) {
                        case 0:
                            Log.d("TAG", "0");
                            break;
                        case 1:
                            Log.d("TAG", "1");
                            break;
                        default:
                            break;
                    }
                }
                //从选中到不再选中时回调
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    switch (tab.getPosition()) {
                        case 0:
                            Log.d("TAG", "onTabUnselected 0");
                            break;
                        case 1:
                            Log.d("TAG", "onTabUnselected 1");
                            break;
                        default:
                            break;
                    }
                }
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
                    Log.d("TAG", "onTabReselected");
                }
            });
        }
    
    }
    

    相关文章

      网友评论

        本文标题:TabLayout自定义tab并实现tab点击切换

        本文链接:https://www.haomeiwen.com/subject/svaxqxtx.html