一言不合就上图
一般的控件都可以设置选择器:即选择改变样式
包括textView,imageView,button等等…
首先创建选择器,在src—main—res—drawable—
右键new—Drawable resource file:
创建起名字:
//这是图片选择器:draw_top_image.xml
//checked选中(true)一种状态的图片样式
<item android:drawable="@mipmap/ic_launcher_round" android:state_checked="true"></item>
//checked未选中(false)一种状态的图片样式
<item android:drawable="@mipmap/ic_launcher" android:state_checked="false"></item>
//这是文本颜色选择器:text_color.xml
//checked选中(true)一种状态的图片样式
<item android:color="@color/colorAccent" android:state_checked="true"></item>
//checked未选中(false)一种状态的图片样式
<item android:color="@color/colorPrimary" android:state_checked="false"></item>
//布局文件:
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/shou"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"//权重
android:button="@null"//去圆圈
android:drawableTop="@drawable/draw_top_image"//图片选择器在这里加
android:gravity="center_horizontal"//文字居中
android:text="首页"
android:textColor="@drawable/text_color"//文字的颜色选择器在这里加
/>
<RadioButton
android:id="@+id/fen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/draw_top_image"
android:gravity="center_horizontal"
android:text="分类"
android:textColor="@drawable/text_color" />
<RadioButton
android:id="@+id/gou"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/draw_top_image"
android:gravity="center_horizontal"
android:text="购物"
android:textColor="@drawable/text_color" />
<RadioButton
android:id="@+id/wode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/draw_top_image"
android:gravity="center_horizontal"
android:text="个人"
android:textColor="@drawable/text_color" />
</RadioGroup>
完事儿
强调几点:
1我的Android studio是2.3.3版本,SDK下-v7:26.+’
compile 'com.android.support:appcompat-v7:26.+'

本文介绍了如何使用RadioButton控件设置选择器,通过选择器改变样式,实现类似底部导航栏的效果。在Android Studio 2.3.3中,可以通过创建Drawable资源文件来定义选择器,如draw_top_image.xml(图片选择器)和text_color.xml(文本颜色选择器),并应用于布局文件中。
562

被折叠的 条评论
为什么被折叠?



