我希望我的底部导航栏在按下时改变背景颜色(仅选定区域),
就像下面的链接一样:
http://uupload.ir/files/nq3k_tab_bar.jpg http://uupload.ir/files/nq3k_tab_bar.jpg
这是我的选择器 xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:state_checked="true" android:color="@color/beyez" />
<item android:state_active="true" app:itemBackground="@color/backbeyez"/>
<item android:color="@color/colorPrimaryDark" />
</selector>
这是该活动的 xml:
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomnav"
android:layout_width="382dp"
android:layout_height="52dp"
android:layout_marginBottom="0dp"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/nav_items_color"
app:itemTextColor="@drawable/nav_items_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"
tools:layout_editor_absoluteY="515dp">
</android.support.design.widget.BottomNavigationView>
这是我的 Java 类的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchorders();
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomnav);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.navigation_orders:
switchorders();
break;
case R.id.navigation_credit:
switchcredits();
break;
case R.id.navigation_works:
switchworks();
break;
case R.id.navigation_profile:
switchprofile();
break;
}
return true;
}
});
}
谢谢大家的帮助!
换一下线就可以了app:itemBackground="@color/colorPrimary"
在 xml 中BottomNavigationView
成为一个可绘制对象,就像您对其他参数所做的那样(例如app:itemTextColor
).
Edit
这应该可行,我用 SDK > 19 尝试过
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_blue_bright" android:state_checked="true"/>
<item android:drawable="@android:color/holo_orange_light"/>
</selector>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)