【发布时间】:2018-09-24 00:06:03
【问题描述】:
我正在尝试在 android 中制作自定义底部导航。 我想禁用转变并为图标提供自定义颜色。 图标最初为灰色,选中的图标变为蓝色。 问题是所有图像即使被选中也保持灰色。
我试图弄乱其中的一些,但没有什么能让按钮在选择时改变颜色。
下面是我试过的代码。
这是java的主要活动
public class admin extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode((bottomNavigationView));
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
bottomNavigationView.setItemIconTintList(null);
urgent urgentfragment = urgent.newInstance();
setFragment(urgentfragment,false);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.bottom_urgent:
urgent urgentfragment = urgent.newInstance();
setFragment(urgentfragment,false);
return true;
case R.id.bottom_all:
all allfragment = all.newInstance();
setFragment(allfragment,false);
item.setChecked(true);
return true;
case R.id.bottom_attendedto:
attendedto attendedtofragment = attendedto.newInstance();
setFragment(attendedtofragment,false);
item.setChecked(true);
return true;
}
return false;
}
};
public void setFragment(Fragment fragment, boolean addtobackstack) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.event_framelayout, fragment);
if(!addtobackstack)
{
for(int i=0;i< fragmentManager.getBackStackEntryCount();++i)
fragmentManager.popBackStack();
}
else
{
fragmentTransaction.addToBackStack(null);
}
fragmentTransaction.commit();
}
}
这是主要的活动xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".admin">
<FrameLayout
android:id="@+id/event_framelayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/navigation"
app:itemIconTint="@color/colors_bottom_navigation"
app:itemTextColor="@color/colors_bottom_navigation"
app:itemBackground="@color/colorPrimaryDark"/>
</android.support.constraint.ConstraintLayout>
这是帮助禁用班次的辅助类
package com.prototypeapp.healer.healer.customviews;
import android.annotation.SuppressLint;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.util.Log;
import java.lang.reflect.Field;
public class BottomNavigationViewHelper {
@SuppressLint("RestrictedApi")
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
【问题讨论】:
标签: android bottomnavigationview