您的位置:首页 > 移动开发 > Android开发

Android底部导航栏点击切换变色

2018-02-06 10:33 477 查看
 底部导航栏切换是由RadioGroup中的RadioButton点击事件切换的,其实之前的文章中已经介绍了,但是在这里详细说明一下。

1.1 底部导航栏的布局文件bottom_bar.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bottom_bar"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/bottom_bar_background"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:tint="@color/qmui_config_color_gray_4">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/qmui_config_color_10_pure_black"
/>
<RadioGroup
android:id="@+id/main_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">

<RadioButton
android:id="@+id/main_rab_house"
android:checked="true"
android:text="主页"
style="@style/main_rbt"
android:drawableTop="@drawable/home_drawable"/>

<RadioButton
android:id="@+id/main_rab_connect"
style="@style/main_rbt"
android:text="连接"
android:drawableTop="@drawable/dashboard_drawable"
/>
<RadioButton
android:id="@+id/main_rab_wifi"
style="@style/main_rbt"
android:text="配置"
android:drawableTop="@drawable/notification_drawable"/>
</RadioGroup>

</LinearLayout>

</LinearLayout>

每个RadioButton的drawableTop表示文字上面的图片,我们在drawable文件下分别建home_drawable.xml,dashboard_drawable.xml和notification_drawable.xml文件。其中home_drawable.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/ic_home_gray_24dp" ></item>
<item android:state_checked="false" android:drawable="@drawable/ic_home_blue_24dp"></item>
</selector>

 表示RadioButton不同状态图片的颜色变化。其他两个文件内容也是一样,这样就能实现点击RadioButton改变

颜色,不需要再代码中显示的改变颜色。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: