android radiobutton底部导航,Android底部导航栏之RadioButton

本文介绍了用Android的RadioButton实现底部导航栏的方法。原理是用RadioButton布局导航栏,处理点击事件动态替换Fragment,用到了RadioButton和StateListDrawable。还给出了布局编写和处理逻辑的代码示例,同时提醒图片不宜过大。

一. 简介

1.1 原理

就是用RadioButton实现一组导航栏的布局,然后处理点击事件,动态替换Fragment

1.2 用到东西

RadioButton

StateListDrawable

1.3 注意事项

图片不能太大,太大会导致效果不好,如图

ef14760b7c31

图标太大的导航栏

二. 实现

编写布局

activity_main.xml

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="cn.foxnickel.radiobuttonnavigation.MainActivity">

android:id="@+id/radio_group"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:background="@android:color/white"

android:orientation="horizontal">

android:id="@+id/rb_home"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:background="@null"

android:button="@null"

android:checked="true"

android:drawableTop="@drawable/radiobutton_bg_home"

android:gravity="center"

android:padding="8dp"

android:text="Home"

android:textSize="16sp"/>

android:id="@+id/rb_like"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:background="@null"

android:button="@null"

android:drawableTop="@drawable/radiobutton_bg_like"

android:gravity="center"

android:padding="8dp"

android:text="Like"

android:textSize="16sp"/>

android:id="@+id/rb_location"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:background="@null"

android:button="@null"

android:drawableTop="@drawable/radiobutton_bg_location"

android:gravity="center"

android:padding="8dp"

android:text="Location"

android:textSize="16sp"/>

android:id="@+id/rb_me"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:background="@null"

android:button="@null"

android:drawableTop="@drawable/radiobutton_bg_me"

android:gravity="center"

android:padding="8dp"

android:text="Me"

android:textSize="16sp"/>

android:layout_width="match_parent"

android:layout_height="2dp"

android:background="#eeeeee"

android:layout_above="@id/radio_group"/>

其中用到的drawableTop为StateListDrawable

content_main.xml

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"

tools:showIn="@layout/activity_main">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/app_name"

android:textSize="24sp"

android:textColor="@color/colorPrimary"/>

处理逻辑

MainActivity

package cn.foxnickel.radiobuttonnavigation;

import android.os.Bundle;

import android.support.v4.content.ContextCompat;

import android.support.v7.app.AppCompatActivity;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

RadioButton mHome,mLike,mLocation,mMe;

RadioGroup mRadioGroup;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView(){

mHome = (RadioButton) findViewById(R.id.rb_home);

mLike = (RadioButton) findViewById(R.id.rb_like);

mLocation = (RadioButton) findViewById(R.id.rb_location);

mMe = (RadioButton) findViewById(R.id.rb_me);

mRadioGroup = (RadioGroup) findViewById(R.id.radio_group);

mRadioGroup.setOnCheckedChangeListener(this);

}

/*RadioGroup点击事件*/

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

switch (checkedId) {

case R.id.rb_home:

// TODO: 2017/4/9 进行fragment的替换

Toast.makeText(MainActivity.this,"Home",Toast.LENGTH_SHORT).show();

break;

case R.id.rb_location:

// TODO: 2017/4/9 进行fragment的替换

Toast.makeText(MainActivity.this,"Location",Toast.LENGTH_SHORT).show();

break;

case R.id.rb_like:

// TODO: 2017/4/9 进行fragment的替换

Toast.makeText(MainActivity.this,"Like",Toast.LENGTH_SHORT).show();

break;

case R.id.rb_me:

// TODO: 2017/4/9 进行fragment的替换

Toast.makeText(MainActivity.this,"Me",Toast.LENGTH_SHORT).show();

break;

}

setTabState();//每次点击之后设置RadioButton的颜色状态

}

private void setTabState() {

setHomeState();

setLocationState();

setLikeState();

setMeState();

}

/*设置HomeRadioButton的状态*/

private void setHomeState() {

if (mHome.isChecked()) {

mHome.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));

} else {

mHome.setTextColor(ContextCompat.getColor(this, R.color.black));

}

}

/*设置LocationRadioButton的状态*/

private void setLocationState() {

if (mLocation.isChecked()) {

mLocation.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));

} else {

mLocation.setTextColor(ContextCompat.getColor(this, R.color.black));

}

}

/*设置LikeRadioButton的状态*/

private void setLikeState() {

if (mLike.isChecked()) {

mLike.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));

} else {

mLike.setTextColor(ContextCompat.getColor(this, R.color.black));

}

}

/*设置MeRadioButton的状态*/

private void setMeState() {

if (mMe.isChecked()) {

mMe.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));

} else {

mMe.setTextColor(ContextCompat.getColor(this, R.color.black));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值