做Fragment导航的时候,用Button做的标签,需要Button有一个选中保持的状态,网上找了一些资料 ,解决了问
registered_seletor.xml
<span style="font-size:18px;"><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/registered_color_bg" android:state_pressed="true"></item>
<item android:drawable="@color/registered_color_bg" android:state_selected="true"></item>
<item android:drawable="@drawable/titlebg" android:state_enabled="true"></item>
</selector></span>
上边三个item是有顺序的,按从上到下以此编译,所以不要弄错了,前两个item的drawable是按下的背景,enabled是未按下的背景,有点不理解这样的原因,有哪位高手知道不妨告知下,感谢
main.xml
<span style="font-size:18px;"><Button
android:id="@+id/btn_goodsdisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品\n展示"
android:layout_weight="1"
android:background="@drawable/registered_selector"
android:textColor="@color/while_color"
/></span>
这里只列一个Button,其他同样
java代码
其中一个选中,其他设置为未选中
<span style="font-size:18px;">public void setBtnBg(int index) {
for (int i = 0; i < btnViews.length; i++) {
if (i == index){
btnViews[i].setSelected(true);
}
else {
btnViews[i].setSelected(false);
}
}
}</span>
这里是效果图