Android使用RadioGroup+RadioButton实现导航栏

主页面代码

package com.example.taobaounion.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.taobaounion.R;
import com.example.taobaounion.utils.LogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TextActivity extends Activity {

    @BindView(R.id.test_navigation_bar)
    public RadioGroup navigation_bar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        ButterKnife.bind(this);
        initListener();
    }

    private void initListener() {
        navigation_bar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            //对控件ID进行判断,执行相应的事件
                switch (checkedId){
                    case R.id.test_home:
                        LogUtils.d(TextActivity.class,"首页");
                        break;
                    case R.id.test_red_packet:
                        LogUtils.d(TextActivity.class,"特惠");
                        break;
                    case R.id.test_select:
                        LogUtils.d(TextActivity.class,"精选");
                        break;
                    case R.id.test_search:
                        LogUtils.d(TextActivity.class,"搜索");
                        break;
                }
            }
        });
    }
}

主页面布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <RadioButton
            android:id="@+id/test_home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_navigation_home"
            android:gravity="center"
            android:checked="true"
            android:text="首页"
            android:textColor="@drawable/naviagation_bar_color" />

        <RadioButton
            android:id="@+id/test_select"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_navigation_select"
            android:gravity="center"
            android:text="精选"
            android:textColor="@drawable/naviagation_bar_color" />

        <RadioButton
            android:id="@+id/test_red_packet"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_navigation_redpacket"
            android:gravity="center"
            android:text="特惠"
            android:textColor="@drawable/naviagation_bar_color" />

        <RadioButton
            android:id="@+id/test_search"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_navigation_search"
            android:gravity="center"
            android:text="搜索"
            android:textColor="@drawable/naviagation_bar_color" />

    </RadioGroup>
    <!--在导航栏上加一条线-->
    <View
        android:layout_width="wrap_content"
        android:layout_height="1px"
        android:layout_marginBottom="10dp"
        android:background="@color/gray"
        android:layout_above="@id/test_navigation_bar"
        />
</RelativeLayout>

分别对四个icon进行状态设置,选中的时候为黄色,按压的时候为灰色,默认为黑色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--state_pressed:表示当按压的时候的变化 state_checked:表示当选中的时候-->
<item android:drawable="@mipmap/home_checked" android:state_checked="true"/>

<item android:drawable="@mipmap/home_press" android:state_pressed="true"/>
<item android:drawable="@mipmap/home_normal"/>
</selector>

在这里插入图片描述

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值