2015-06-27 41 views
0

我已经使用工具栏和使用过的设计库。工具栏导航图标和后退箭头图标在Android 5.0设备上显示为白色,但在Android 4.x设备上显示为黑色图标。如何在Android 5.0和Android 4.x设备上将图标显示为白色。工具栏后退箭头在4.x设备上显示为黑色

MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 


    final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha); 

    upArrow.setColorFilter(getResources().getColor(android.R.color.white), PorterDuff.Mode.SRC_ATOP); 

    toolbar.setNavigationIcon(upArrow); 

    setSupportActionBar(toolbar); 

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar, R.string.app_name, R.string.app_name){ 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
      fab1.setVisibility(View.VISIBLE); 
     } 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
      fab1.setVisibility(View.GONE); 
     } 
    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    mDrawerToggle.syncState(); /* if comment this line white color applied,but only arrow displayed,not display nav icon(three line icon)*/ 
} 


@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 

toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<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_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:minHeight="?attr/actionBarSize" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:titleTextAppearance="@style/AppTheme.Toolbar.Title" 
    app:elevation="@dimen/cardview_default_elevation" 
    app:borderWidth="0dp" 
    > 

值\ styles.xml

<style name="AppTheme.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title"> 
     <!-- Set proper title size --> 
     <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item> 
     <!-- Set title color --> 
     <item name="android:textColor">@color/toolbar_title</item> 
</style> 

个值-V21 \ styles.xml

<style name="AppTheme.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title"> 
     <!-- Set proper title size --> 
     <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item> 
     <!-- Set title color --> 
     <item name="android:textColor">@color/toolbar_title</item> 
</style> 

回答

0

你已经张贴了答案:)

//final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha); 

    //upArrow.setColorFilter(getResources().getColor(android.R.color.white), PorterDuff.Mode.SRC_ATOP); 

    //toolbar.setNavigationIcon(upArrow); 

这是作品。测试4.4.2

我觉得你有这么多的尝试,有混乱。

+0

我已在4.2.1设备上测试过,但没有正常工作... – Ramprasad

+0

@Ramprasad您尝试删除其他已评论的代码吗? 它适合我。也许你会在'* .xml'主题文件中设置颜色? –

+0

@Ramprasad如果更改代码中的返回按钮颜色,将会更好,您需要删除'styles.xml'和'themes.xml'中的所有匹配项。它可能会产生冲突 –

相关问题