TabLayout自定义布局(标题实现三个颜色)

昨天和朋友遇到一个问题,说她想要TabLayout+viewpager想实现标题有三个颜色的目的(选中和不选中状态的颜色很简单,可是人家有一个标题栏下的功能未开放哈,必须要用一个不同的颜色来显示) 

朋友有难,就要“支援”奋斗这不扛着“义气”的大气害羞就写下了这篇大笑

还是老方法,直接上代码

1.布局

 tab_layout.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"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        >

    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

</LinearLayout>

 

 

 

 

tab_item_layout.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/black_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hhhh"
        android:textSize="20sp"
        android:padding="10dp"
        android:textColor="#000"
        />
    <TextView
        android:id="@+id/gray_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="www"
        android:textSize="20sp"
        android:padding="10dp"
        android:textColor="#DDD"
        />

</LinearLayout>

 

 

 

 

2.Activity

 

package com.ylz.TabLayout;


import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

import com.ylz.floatview.R;

/**
 * Created by Administrator on 2016/10/27.
 */
public class TabLayoutActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewpager;
    String[] titleName={"One","Tow","Three"};
    TabAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_layout);
        init();
    }

    private void init() {
        tabLayout=(TabLayout)findViewById(R.id.tablayout);
        viewpager=(ViewPager)findViewById(R.id.viewpager);
        adapter = new TabAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewpager);

        for(int i=0;i<tabLayout.getTabCount();i++){
            TabLayout.Tab tab=tabLayout.getTabAt(i);
            if(i==0) {
                tab.setCustomView(adapter.getTabView(i));
            }else{
                tab.setCustomView(adapter.getTabView(i));
            }
        }

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ((TextView)tab.getCustomView().findViewById(R.id.black_tv)).setTextColor(Color.RED);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ((TextView)tab.getCustomView().findViewById(R.id.black_tv)).setTextColor(Color.BLACK);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) { 
                ((TextView)tab.getCustomView().findViewById(R.id.black_tv)).setTextColor(Color.RED);
            }
        });

    }

    private  class TabAdapter extends FragmentPagerAdapter{
        public TabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new Fragment();
        }

        @Override
        public int getCount() {
            return 3;
        }
        TextView blackTv;
        TextView grayTv;
        public View getTabView(int position){
            View v= LayoutInflater.from(TabLayoutActivity.this).inflate(R.layout.tab_item_layout,null);
            blackTv=(TextView)v.findViewById(R.id.black_tv);
            grayTv=(TextView)v.findViewById(R.id.gray_tv);
            if(position==2){
                blackTv.setVisibility(View.GONE);
                grayTv.setVisibility(View.VISIBLE);
                grayTv.setText(titleName[position]);
            }else{
                blackTv.setVisibility(View.VISIBLE);
                grayTv.setVisibility(View.GONE);
                blackTv.setText(titleName[position]);
            }
            return v;
        }

    }
}
 
最终效果图:

 

 

 

 

PS:本文只是实现了那个需求,有些优化还需要在使用的时候考虑。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值