页面切换显示横线

在这里插入图片描述

1.布局

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.design.widget.TabLayout
            android:id="@+id/data_tl_tablayout"
            android:layout_width="match_parent"
            android:background="#fff"
            android:layout_height="0dp"
            app:tabIndicatorColor="#fff"
            android:layout_weight="1"/>
        <android.support.v4.view.ViewPager
            android:id="@+id/data_vp_viewpager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="11"/>
    </LinearLayout>

2.Activity代码

public class DataFragment extends Fragment {

    private TabLayout data_tl_tablayout;
    private ViewPager data_vp_viewpager;
    private List<Fragment> mFragments;
    private DataFragmentPageAdapter mDataFragmentPageAdapter;
    boolean flag;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_data, container, false);
        initView(inflate);
        initTabLayout();
        return inflate;
    }

    private void initTabLayout() {
        mFragments = new ArrayList<>();
        mFragments.add(new VideoDataFragment());
        mFragments.add(new PersonalDataFragment());
        mDataFragmentPageAdapter = new DataFragmentPageAdapter(getChildFragmentManager());
        mDataFragmentPageAdapter.setFragments(mFragments);
        data_vp_viewpager.setAdapter(mDataFragmentPageAdapter);

        data_tl_tablayout.addTab(data_tl_tablayout.newTab());
        data_tl_tablayout.addTab(data_tl_tablayout.newTab());

        data_tl_tablayout.setupWithViewPager(data_vp_viewpager);

        data_tl_tablayout.getTabAt(0).setCustomView(getTabView("视频数据",flag = true,"#2bc17a"));
        data_tl_tablayout.getTabAt(1).setCustomView(getTabView("用户数据",flag = false,"#000000"));
        data_tl_tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                data_vp_viewpager.setCurrentItem(tab.getPosition());
                changeTabSelect(tab);
            }

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

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

            }
        });
    }

    private void changeTabNormal(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        TextView tab_icon = (TextView) view.findViewById(R.id.tab_icon);
        TextView tab_title = (TextView) view.findViewById(R.id.tab_title);
        tab_title.setTextColor(getResources().getColor(R.color.colorBluck));
        if (tab_title.getText().toString().equals("视频数据")) {
            tab_icon.setVisibility(View.INVISIBLE);
        } else{
            tab_icon.setVisibility(View.INVISIBLE);
        }
    }

    private void changeTabSelect(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        TextView tab_icon = (TextView) view.findViewById(R.id.tab_icon);
        TextView tab_title = (TextView) view.findViewById(R.id.tab_title);
        tab_title.setTextColor(getResources().getColor(R.color.colorGreen));
        if (tab_title.getText().toString().equals("视频数据")) {
            tab_icon.setVisibility(View.VISIBLE);
        } else{
            tab_icon.setVisibility(View.VISIBLE);
        }
    }


    public View getTabView(String title,boolean flag,String color) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_icon, null);
        TextView tab_title = (TextView) v.findViewById(R.id.tab_title);
        tab_title.setText(title);
        tab_title.setTextColor(Color.parseColor(color));
        TextView tab_icon = (TextView) v.findViewById(R.id.tab_icon);
        if (flag){
            tab_icon.setVisibility(View.VISIBLE);
        }else {
            tab_icon.setVisibility(View.INVISIBLE);
        }
        return v;
    }

    private void initView(View inflate) {
        data_tl_tablayout = (TabLayout) inflate.findViewById(R.id.data_tl_tablayout);
        data_vp_viewpager = (ViewPager) inflate.findViewById(R.id.data_vp_viewpager);
    }
}

3:按钮布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">



    <TextView
        android:id="@+id/tab_title"
        android:textSize="16sp"
        android:text="aaa"
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"/>
    <TextView
        android:id="@+id/tab_icon"
        android:visibility="visible"
        android:layout_marginTop="5dp"
        android:layout_width="33dp"
        android:layout_height="4dp"
        android:background="@drawable/share_tablay_bg"/>

</LinearLayout>

4.横线背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ff2bc17a" />
    <corners android:topLeftRadius="2dp" android:topRightRadius="2dp" android:bottomLeftRadius="2dp" android:bottomRightRadius="2dp" />
</shape>
发布了57 篇原创文章 · 获赞 12 · 访问量 8305
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览