Android笔记——自定义TabLayout之title与icon

自定义TabLayout

这里写图片描述

总布局

实现的是底端TabLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_content_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/tl_tab_home"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tl_tab_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        app:tabIndicatorHeight="0dp"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/darkgreen"
        android:layout_alignParentBottom="true"
        android:elevation="@dimen/elevation_5"/>

</RelativeLayout>

TabLayout自定义布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ImageView
        android:id="@+id/tab_icon"
        android:layout_width="@dimen/back_size"
        android:layout_height="@dimen/back_size"/>

    <TextView
        android:id="@+id/tab_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@drawable/selector_text_tab"
        android:textSize="@dimen/text_size_15"
        android:enabled="false"/>

</LinearLayout>

Selector

color

<item  android:state_selected="true" android:color="@color/darkgreen"></item>
<item  android:color="@color/darkgray"></item>
<item  android:state_pressed="true" android:color="@color/darkgreen"/>
<item  android:state_enabled="true" android:color="@color/darkgreen"/>


icon

因为三个图标的selector是一样的,所以就取了一个

<item android:state_selected="true" android:drawable="@drawable/icon_dynamics_select"></item>
<item android:drawable="@drawable/icon_dynamics_unselect"></item>
<item android:state_pressed="true" android:drawable="@drawable/icon_dynamics_select"/>

代码

主要分以下两步实现,若想看怎么自定义,请直接跳到最后的getTabView

initContent();
initTab();
String[] title = {"个人","推荐","动态"};

ContentPagerAdapter

class ContentPagerAdapter extends FragmentPagerAdapter {

        public ContentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return tabFragments.get(position);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return tabIndicators.get(position);
        }
}

initContent

private void initContent(){
        tabIndicators = new ArrayList<>();
        for(int i = 0; i < title.length; i++){
            tabIndicators.add(title[i]);
        }
        tabFragments = new ArrayList<>();
        tabFragments.add(new UserFragment());
        tabFragments.add(new RecommendFragment());
        tabFragments.add(new DynamicsFragment());

        contentAdapter = new ContentPagerAdapter(getSupportFragmentManager());
        vp_home.setAdapter(contentAdapter);
}

initTab

private void initTab(){
        tab_home.setTabMode(TabLayout.MODE_FIXED);
        tab_home.setSelectedTabIndicatorHeight(0);
        ViewCompat.setElevation(tab_home,getResources().getDimension(R.dimen.elevation_5));
        tab_home.setupWithViewPager(vp_home);
        for(int i = 0; i < title.length;i++){
            TabLayout.Tab tab = tab_home.getTabAt(i);
            if(tab != null){
                tab.setCustomView(getTabView(i));
            }
        }
        tab_home.getTabAt(1).select();
}

getTabView

private View getTabView(int position){
        TypedArray prodIcon_tab = getResources().obtainTypedArray
                (R.array.prodIcon_tab);//图片资源ID数组
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item,null);
        TextView tab_text = (TextView)view.findViewById(R.id.tab_text);
        tab_text.setText(title[position]);
        ImageView tab_icon = (ImageView)view.findViewById(R.id.tab_icon);
        tab_icon.setImageResource(prodIcon_tab.getResourceId(position,0));
        return view;
}

我的简书地址:https://www.jianshu.com/p/2b5c0ef2c22a

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值