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>
我已在4.2.1設備上測試過,但沒有正常工作... – Ramprasad
@Ramprasad您嘗試刪除其他已評論的代碼嗎? 它適合我。也許你會在'* .xml'主題文件中設置顏色? –
@Ramprasad如果更改代碼中的返回按鈕顏色,將會更好,您需要刪除'styles.xml'和'themes.xml'中的所有匹配項。它可能會產生衝突 –