我正在導航的抽屜我的應用程序。 我製作了導航抽屜#000000(黑色)的背景。每當我選擇任何項目文本的文本顏色從#E44F50(胡蘿蔔紅色)變爲黑色,但我不能改變所選項目的背景顏色。 我想要的只是將所選項目的背景顏色從黑色改爲胡蘿蔔紅色。如何更改選定項的背景顏色的抽屜式導航
這裏是GitHub的鏈接到我的APP-
https://github.com/manmeet-22/NewsSnips-app.git
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorPrimary"
app:headerLayout="@layout/header"
app:itemIconTint="@drawable/drawer_item" //changing text color
app:itemTextAppearance="@style/TextAppearance20"
app:itemTextColor="@color/drawer_item"
app:itemBackground="@drawable/drawer_selected_item" //trying to change backgroundcolor the same way
app:menu="@menu/drawer"
tools:context="com.androidexample.newssnips.app.NavigationDrawerFragment"/>
</android.support.v4.widget.DrawerLayout>
在此我試圖改變背景顏色像我一樣的文字,但我的應用程序崩潰。
這裏是我的drawer_selected_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_selected="true"/>
<item android:color="@android:color/transparent"/>
</selector>
這裏是我的drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_checked="true" />
<item android:color="@color/colorAccent" />
</selector>
這裏是我的MainActivity.java:
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setItemIconTintList(null);
Menu menu = navigationView.getMenu();
//For chnaging the textColor and textSize of group's Name ie. Category
MenuItem categoryName= menu.findItem(R.id.categoryGroup);
SpannableString s = new SpannableString(categoryName.getTitle());
s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance25), 0, s.length(), 0);
//For changing the Text of action bar as the selected category
categoryName.setTitle(s);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked())
menuItem.setChecked(false);
else
menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and open the Appopriate news accordingly
switch (menuItem.getItemId()) {
case R.id.itemWorld:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=world";
setTitle("World News");
basicDefaults();
return true;
case R.id.itemFootball:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=football";
setTitle("Football News");
basicDefaults();
return true;
case R.id.itemFashion:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=fashion";
setTitle("Fashion News");
basicDefaults();
return true;
case R.id.itemSports:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=sport";
setTitle("Sports News");
basicDefaults();
return true;
case R.id.itemBusiness:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=business";
setTitle("Business News");
basicDefaults();
return true;
case R.id.itemTechnology:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=technology";
setTitle("Technology News");
basicDefaults();
return true;
case R.id.itemOpinion:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=commentisfree";
setTitle("Opinions");
basicDefaults();
return true;
case R.id.itemCulture:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=culture";
setTitle("Culture News");
basicDefaults();
return true;
case R.id.itemTravel:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=travel";
setTitle("Travel News");
basicDefaults();
return true;
case R.id.itemLifestyle:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=lifeandstyle";
setTitle("LifeStyle News");
basicDefaults();
return true;
case R.id.itemEnvironment:
GAURDIAN_REQUEST_URL = "http://content.guardianapis.com/search?api-key=f51fa87d-8a55-4fc1-b552-8fa6eb98dee4§ion=environment";
setTitle("Environment News");
basicDefaults();
return true;
default:
Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
return true;
}
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
PS.-我曾嘗試實現其他的方法太像
將這添加到styles.xml文件中的apptheme中。
<item name="android:activatedBackgroundIndicator">@drawable/drawer_selected_item</item>
但我還是找不到我的解決方案。
請幫助它是在我的項目留下的最後一件事。
請參考[LINK1](http://stackoverflow.com/questions/30886453/change-the-color-of-a-checked-menu-item-in-a-導航抽屜),[LINK2](http://stackoverflow.com/questions/22374660/navigation-drawer-item-background-colour-for-selected-item)和[LINK3(http://stackoverflow.com/問題/ 23112376/custom-background-color-for-selected-item-with-activatedbackgroundindicator-na)它會幫助你。 –
@vijaychhalotre我已經看到了這些,但我仍然不能改變選定項目的背景顏色。 –