底部选项卡

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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.fanmeihua.fanmeihua20171016.MainActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_above="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="match_parent">android.support.v4.view.ViewPager>

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/r1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="首页" />

        <RadioButton
            android:id="@+id/r2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="想法" />

        <RadioButton
            android:id="@+id/r3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.06"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="市场" />

        <RadioButton
            android:id="@+id/r4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="通知" />

        <RadioButton
            android:id="@+id/r5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="更多" />
    RadioGroup>

RelativeLayout>

监听事件:

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        for (int j = 0; j <rlist.size() ; j++) {
            int id = rlist.get(j).getId();
            if (id == i){
                vp.setCurrentItem(j);
            }
        }
    }
});
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i <rlist.size() ; i++) {
            if (i==position){
                rlist.get(i).setTextColor(Color.RED);
            }else{
                rlist.get(i).setTextColor(Color.BLACK);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

你可能感兴趣的:(android)