更改工具栏中的图标颜色 [英] Change back icon color in toolbar
问题描述
我将 Android 组件 navigation
用于带有 NavigationView
的 DrawerLayout
.
I'm using the Android component navigation
for a DrawerLayout
with NavigationView
.
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
LoginActivity.toClose.finish();
LoginViewModel viewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
Toolbar toolbar = findViewById(R.id.toolbar_menu);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
assert actionbar != null;
drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view_menu);
navigationView.inflateMenu(viewModel.setUserInterface());
NavController navController = Navigation.findNavController(this, R.id.fragment_main);
NavigationUI.setupWithNavController(navigationView, navController);
NavigationUI.setupActionBarWithNavController(this,navController,drawerLayout);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
}
一切正常,只是向上按钮"的颜色与我在操作栏中的标题颜色不同.
Everything's ok, except that the "Up button" are in a different color that my title in The action bar.
工具栏的 XML 是:
The XML for the Toolbar is:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:textColor="#FFF"
android:textColorPrimary="#FFF"
android:theme="@style/Toolbar"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="#FFF"
app:title="@string/app_name"/>
标题是白色的,但图标是黑色的.
The title is white, but the icon is black.
我的问题是:如何更改此图标的颜色?
My question is: how can I change the color of this icon?
即使我将主颜色更改为白色并且主题编辑器以白色显示图标,当应用程序运行时,颜色仍然是黑色.
Even if I change the primary color to white and the theme editor show me the icon in white, when the app is running, the color is still black.
我正在构建的应用程序有 minSdkVersion 15我在带有 API 7 SDK 24 的手机中运行它.我还没有在带有 SDK 15 的模拟器中运行.
The app that I'm building has minSdkVersion 15 and I run it in a phone with API 7 SDK 24. I didn't run in the emulator with SDK 15 yet.
推荐答案
使用这种风格
<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<!-- Customize color of Toolbar -->
<item name="colorControlNormal">@color/WhiteColor</item>
</style>
然后在工具栏 XML 的 app:theme="@style/ToolbarTheme"
中使用它:
and then use it in app:theme="@style/ToolbarTheme"
in your Toolbar XML :
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:textColor="#FFF"
android:textColorPrimary="#FFF"
app:theme="@style/ToolbarTheme"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="#FFF"
app:title="@string/app_name"/>
这篇关于更改工具栏中的图标颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!