导航菜单图标颜色和文本在android中没有改变
Posted
技术标签:
【中文标题】导航菜单图标颜色和文本在android中没有改变【英文标题】:navigation menu icon color and text is not getting changed in android 【发布时间】:2020-07-28 02:57:13 【问题描述】:在 manin 活动 xml 中的 cdoe 中,我在这里使用“android.support.design.widget.BottomNavigationView”,我在页脚中显示了一些菜单图标。在选择每个图标时,我正在导航到不同的屏幕。 现在我必须更改我可以在 activity_main xm 中执行的 BottomNavigationView 的背景,更改背景颜色,现在我必须将菜单图标 colot 更改为白色,文本颜色应该是白色的。我在菜单导航 xml @drawable/icon_home 中进行了更改,我将颜色更改为白色,但它没有出现。图标下方的事实文本也不是白色的。请帮忙 。建议我。谢谢你
// XML 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/container"
android:layout_
android:layout_
android:orientation="vertical"
tools:context="com.tecnotree.selfcare.ui.main.MainActivity">
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_
android:layout_
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</LinearLayout>
//菜单导航代码
<item
android:id="@+id/navigation_home"
android:icon="@drawable/icon_home"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_services"
android:icon="@drawable/icon_services"
android:title="@string/service" />
<item
android:id="@+id/navigation_cart"
android:icon="@drawable/icon_cart"
android:title="@string/shop" />
<item
android:id="@+id/navigation_payments"
android:icon="@drawable/icon_credit_card"
android:title="@string/payments" />
<item
android:id="@+id/navigation_more"
android:icon="@drawable/icon_more"
android:title="@string/more" />
/// 活动java代码
public class MainActivity extends AppCompatActivity implements SelfCareManager.CustomerDetailsRequestCallback
// this is a leftover from Farrukh's initial design. TBD whether this actually needed
// @BindView(R.id.message)
// TextView mTextMessage;
@Inject
ManagerFactory managerFactory;
@Inject
VolatileOps volatileOps;
@BindView(R.id.navigation)
BottomNavigationView navigationView;
private Spinner spinner;
private NavigationHelper navigationHelper;
public ProgressDialog loading;
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.toolbar_dropdown, menu);
MenuItem item = menu.findItem(R.id.spinner);
spinner = (Spinner) item.getActionView();
SelfCareManager selfCareManager = managerFactory.getSelfCareManager();
selfCareManager.getLoginCustomerDetails(
volatileOps.getString(VolatileKey.LOGIN_NAME),
volatileOps.getString(VolatileKey.SERVICE_NUMBER),
true,
this);
return true;
@Override
protected void onCreate(Bundle savedInstanceState)
loading = ProgressDialog.show(MainActivity.this, "Fetching Data", " Please Wait...", false, false);
super.onCreate(savedInstanceState);
DaggerMainActivityComponent.builder()
.appComponent(App.getInstance().getApplicationWideDeps())
.build()
.inject(this);
navigationHelper = new NavigationHelper(getSupportFragmentManager());
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
navigationView.setOnNavigationItemSelectedListener(navigationHelper);
navigationHelper.disableShiftMode(navigationView);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new TeaserFragment()).commit();
@Override
public void onResume()
super.onResume();
navigationHelper.reloadCurrentFragment();
@Override
public void onCustomerDetailsReceived(Customer details)
loading.dismiss();
List<String> serviceNumbers = new ArrayList<>();
for(ServiceDetails serviceDetails: details.getServiceDetails())
serviceNumbers.add(serviceDetails.getServiceNumber());
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, serviceNumbers);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
volatileOps.setString(VolatileKey.SERVICE_NUMBER, (String) adapterView.getItemAtPosition(i));
navigationHelper.reloadCurrentFragment();
@Override
public void onNothingSelected(AdapterView<?> adapterView)
);
@Override
public void onCustomerDetailsRequestError(String errorMessage)
loading.dismiss();
Log.d("MainActivity", "Customer details request error: " + errorMessage);
public static class NavigationHelper implements BottomNavigationView.OnNavigationItemSelectedListener
private FragmentManager manager;
private Fragment currentFragment;
private HomeFragment homeFragment;
private ServiceFragment serviceFragment;
private ShopFragment shopFragment;
private MoreFragment moreFragment;
private PaymentsFragment paymentsFragment;
@Inject
ManagerFactory managerFactory;
@Inject
VolatileOps volatileOps;
private HomeScreenContract.Presenter homePresenter;
private ShopScreenContract.Presenter shopPresenter;
private PaymentsScreenContract.Presenter paymentsPresenter;
private ServiceScreenContract.Presenter servicePresenter;
private MoreScreenContract.Presenter morePresenter;
NavigationHelper(FragmentManager manager)
this.manager = manager;
DaggerMainActivityComponent.builder()
.appComponent(App.getInstance().getApplicationWideDeps())
.build()
.inject(this);
this.homeFragment = new HomeFragment();
this.homePresenter = new HomeScreenPresenter(homeFragment, managerFactory, volatileOps);
homeFragment.setPresenter(homePresenter);
this.serviceFragment = new ServiceFragment();
this.servicePresenter = new ServiceScreenPresenter(serviceFragment, managerFactory, volatileOps);
serviceFragment.setPresenter(servicePresenter);
this.shopFragment = new ShopFragment();
this.shopPresenter = new ShopScreenPresenter(shopFragment, managerFactory, volatileOps);
shopFragment.setPresenter(shopPresenter);
this.moreFragment = new MoreFragment();
this.morePresenter = new MoreScreenPresenter(moreFragment, managerFactory, volatileOps);
moreFragment.setPresenter(morePresenter);
this.paymentsFragment = new PaymentsFragment();
this.paymentsPresenter = new PaymentsScreenPresenter(paymentsFragment, managerFactory, volatileOps);
paymentsFragment.setPresenter(paymentsPresenter);
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
switch (item.getItemId())
case R.id.navigation_home:
manager.beginTransaction().replace(R.id.fragment_container, homeFragment).commit();
currentFragment = homeFragment;
return true;
case R.id.navigation_services:
manager.beginTransaction().replace(R.id.fragment_container, serviceFragment).commit();
currentFragment = serviceFragment;
return true;
case R.id.navigation_cart:
manager.beginTransaction().replace(R.id.fragment_container, shopFragment).commit();
currentFragment = shopFragment;
return true;
case R.id.navigation_more:
manager.beginTransaction().replace(R.id.fragment_container, moreFragment).commit();
currentFragment = moreFragment;
return true;
case R.id.navigation_payments:
manager.beginTransaction().replace(R.id.fragment_container, paymentsFragment).commit();
currentFragment = paymentsFragment;
return true;
return false;
public void reloadCurrentFragment()
if(currentFragment != null)
manager.beginTransaction().detach(currentFragment).attach(currentFragment).commit();
else
HomeFragment homeFragment = new HomeFragment();
HomeScreenPresenter homePresenter = new HomeScreenPresenter(homeFragment, managerFactory, volatileOps);
homeFragment.setPresenter(homePresenter);
manager.beginTransaction().replace(R.id.fragment_container, homeFragment).commit();
currentFragment = homeFragment;
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("NavigationHelper", "Unable to get shift mode field", e);
catch (IllegalAccessException e)
Log.e("NavigationHelper", "Unable to change value of shift mode", e);
【问题讨论】:
【参考方案1】:我认为在选中或未选中图标的情况下都可以为您提供帮助
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<bitmap android:src="@drawable/icon_home" android:tint="@color/white" />
</item>
<item android:state_checked="true">
<bitmap android:src="@drawable/icon_home" android:tint="@color/grey" />
</item>
<item>
<bitmap android:src="@drawable/icon_home" android:tint="@color/white" />
</item>
</selector>
【讨论】:
你能检查一下我上面的菜单吗...我正在使用那个\ 经过@drawable/icon "以上是关于导航菜单图标颜色和文本在android中没有改变的主要内容,如果未能解决你的问题,请参考以下文章