简体   繁体   English

更改工具栏中的图标颜色

[英]Change back icon color in toolbar

I'm using the Android component navigation for a DrawerLayout with NavigationView .我将 Android 组件navigation用于DrawerLayoutNavigationView

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.一切正常,除了“向上按钮”与我在操作栏中的标题颜色不同。

The XML for the Toolbar is:工具栏的 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"
    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.即使我将主要颜色更改为白色并且主题编辑器以白色显示图标,当应用程序运行时,颜色仍然是黑色。

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.我正在构建的应用程序具有 minSdkVersion 15,我在带有 API 7 SDK 24 的手机中运行它。我还没有在带有 SDK 15 的模拟器中运行它。

use this style使用这种风格

<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
        <!-- Customize color of Toolbar -->
        <item name="colorControlNormal">@color/WhiteColor</item>
    </style>

and then use it in app:theme="@style/ToolbarTheme" in your Toolbar XML :然后在工具栏 XML 中的app:theme="@style/ToolbarTheme"中使用它:

 <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"/>

Another option, with new Themes (Light/Dark modes) is to use styles and themes.使用新主题(浅色/深色模式)的另一种选择是使用样式和主题。

First approach.第一种方法。

  1. This works if you use default icons and in a fragment (eg) you use following: toolbar.setupWithNavController(findNavController()) , so then in layout you must do following:如果您使用默认图标并且在片段(例如)中使用以下内容,则此方法有效: toolbar.setupWithNavController(findNavController()) ,因此在布局中您必须执行以下操作:
  2. In your app theme in xml:在 xml 中的应用主题中:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...
    <item name="drawerArrowStyle">@style/MyDrawerArrowToggleStyle</item>

Note: This solution is for your own toolbars, which you placed in your layout by yourself, that's why my main app theme is Theme.MaterialComponents.DayNight.NoActionBar .注意:此解决方案适用于您自己放置在布局中的自己的工具栏,这就是为什么我的主要应用主题是Theme.MaterialComponents.DayNight.NoActionBar However, I haven't tried with default ActionBar , so maybe it will work as well.但是,我还没有尝试使用默认的ActionBar ,所以也许它也能工作。

  1. Your drawer arrow/toggle style:您的抽屉箭头/切换样式:
<style name="MyDrawerArrowToggleStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="color">@color/your_color_here</item>
    </style>

And now all default "<-" icons in toolbars will be in your color.现在工具栏中的所有默认“<-”图标都将采用您的颜色。 It should work for Hamburger icons too.它也应该适用于汉堡包图标。 But I didn't check, as I don't have navigation drawer.但我没有检查,因为我没有导航抽屉。

Second approach.第二种方法。

When you want your own icon or action in NavigationOnClickListener .当您想要在NavigationOnClickListener自己的图标或操作时。

  1. In the fragment:在片段中:
toolbar.setNavigationOnClickListener { 
   findNavController().navigateUp()
   //plus another actions if required
}
toolbar.setNavigationIcon(R.drawable.your_icon_here)
  1. In main theme:在主题中:
<item name="toolbarNavigationButtonStyle">@style/MyAppToolbarNavButtonStyle</item>
  1. My style:我的风格:
<style name="MyAppToolbarNavButtonStyle" parent="Widget.AppCompat.Toolbar.Button.Navigation">
        <item name="tint">@color/your_color_here</item>
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM