在 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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.tecnotree.selfcare.ui.main.MainActivity">
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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);
}
}
}
}