如何让导航darwer的图标显示其真实颜色?
我设计了自己的自定义图标,但它们在我的导航器中显示为灰色。
我目前用于整个应用的主题是:Light.DarkActionBar
这是抽屉菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title=""
android:id="@+id/nav_item_inbox"
/>
<item android:title="Home"
android:id="@+id/nav_item_home"
android:icon="@drawable/home"
/>
<item android:title="News">
<menu>
<item android:title="Events News"
android:id="@+id/nav_item_events"
android:icon="@drawable/ic_action_search"
/>
<item android:title="International News"
android:id="@+id/nav_item_international"
android:icon="@drawable/nav_icong"/>
<item
android:title="Climatic News"
android:id="@+id/nav_item_weathernews"
android:icon="@drawable/spam"/>
</menu>
</item>
<item android:title="Climate">
<menu>
<item
android:title="Weather Widget"
android:id="@+id/wewidget"
android:icon="@drawable/bin"/>
</menu>
</item>
<group android:id="@+id/About">
<item android:title="About"
android:id="@+id/abt"
android:icon="@drawable/settings"/>
<item android:title="FeedBack"
android:icon="@drawable/help"/>
<item android:title="Help"
android:icon="@drawable/help"/>
</group>
</menu>
这是activity_main布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="?attr/colorPrimary"
android:animateLayoutChanges="true">
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
>
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerView">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/shitstuff"
app:itemTextColor="@color/black"
app:menu="@menu/drawermenu"
android:background="@android:color/white"
android:layout_marginTop="-24dp"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
这是MainActivity类:
public class MainActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new HomeFragment()).commit();
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_item_home) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new HomeFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_events) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new EventsFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_international) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new InternationalNews()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_weathernews) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new WeatherNews()).commit();
}
if (menuItem.getItemId() == R.id.wewidget) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new Weathermain()).commit();
}
if (menuItem.getItemId() == R.id.abt) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new About()).commit();
}
return false;
}
});
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
//toolbar.setAlpha(0);
//toolbar.setTranslationY(-300);
//toolbar.animate().setDuration(1000).translationY(0).alpha(1);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar, R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
// case R.id.menuitem_recipies:
// startActivity(new Intent(this, Recipies.class));
// Toast.makeText(this, getString(R.string.recs),
// Toast.LENGTH_SHORT).show();
//return true;
case R.id.menuitem_share:
startActivity(new Intent(this, Share.class));
Toast.makeText(this, getString(R.string.ui_menu_share),
Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_feedback:
startActivity(new Intent(this, Feedback.class));
Toast.makeText(this, getString(R.string.ui_menu_feedback),
Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_about:
startActivity(new Intent(this, About.class));
Toast.makeText(this, getString(R.string.ui_menu_about),
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
看看Answer,这对我来说是完美的。
navigationView.setItemIconTintList(null);
因此,在Android代码中,他们将默认为ColorStateList
:
// NavigationView
final ColorStateList itemIconTint;
if (a.hasValue(R.styleable.NavigationView_itemIconTint)) {
itemIconTint = a.getColorStateList(R.styleable.NavigationView_itemIconTint);
} else {
itemIconTint = createDefaultColorStateList(android.R.attr.textColorSecondary);
}