【问题标题】:Change color of DrawerToggle in toolbar on Android在 Android 的工具栏中更改 DrawerToggle 的颜色
【发布时间】:2015-07-21 20:32:34
【问题描述】:

谁能指出为什么我的抽屉开关(用于打开导航抽屉的动画小汉堡图标)拒绝在我身上切换颜色?这导致我有很多停机时间,我似乎无法弄清楚原因。

这是我的主题 - 抽屉切换的颜色为 disabled_default_text。

<style name="Theme.MyApp.NoActionBar" parent="Theme.MyApp.NoActionBar">
    <item name="actionBarStyle">@style/Widget.MyApp.ActionBar</item>
    <item name="colorAccent">@color/cs7</item>
    <item name="colorControlNormal">@color/disabled_default_text</item>
</style>

这是工具栏布局:

<?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:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/ab_solid"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/MyToolbarTheme"
    app:elevation="@dimen/toolbar_elevation"/>

MyToolbarTheme - 我用白色覆盖 colorControlNormal。

<style name="MyToolbarTheme"     parent="@style/Widget.AppCompat.ActionBar.Solid">
    <item name="background">@drawable/ab_solid</item>
    <item name="titleTextStyle">@style/MyTitleStyle</item>
    <item name="colorControlNormal">@color/white</item>
</style>

奇怪的是,添加 colorControlNormal 会将溢出菜单的颜色从 disabled_default_text 颜色更改为白色,但抽屉切换不会改变。谁能弄清楚我做错了什么?

【问题讨论】:

  • Widget.MyApp.ActionBar 是什么?如果你能创建一个MCVE 会有所帮助。

标签: android material-design android-theme android-styles


【解决方案1】:

您可以将drawerArrowStyle添加到您的主题中

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="drawerArrowStyle">@style/MyDrawerArrowToggle</item>
</style>

你可以自定义你的MyDrawerArrowToggle

 <style name="MyDrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="color">@color/white</item>
 </style>

【讨论】:

    【解决方案2】:

    证明这一点:

    style.xml

    <style name="AppTheme" parent="AppTheme.Base"></style>
    
    <style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/theme_primary</item>
        <item name="colorPrimaryDark">@color/theme_dark</item>
        <item name="colorAccent">@color/theme_accent</item>
        <item name="android:windowBackground">@color/theme_wbackground</item>
    </style>
    

    style-v21.xml

    <style name="AppTheme" parent="AppTheme.Base">
        <item name="android:colorPrimary">@color/theme_primary</item>
        <item name="android:colorPrimaryDark">@color/theme_dark</item>
        <item name="android:colorAccent">@color/theme_accent</item>
        <item name="android:windowBackground">@color/theme_wbackground</item>
        <item name="android:navigationBarColor">@color/theme_primary</item>
    </style>
    

    和 int 工具栏: app_bar.xml

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/theme_primary"/>
    

    【讨论】:

      【解决方案3】:

      以上解决方案使用的样式可能会起作用。但是如果您想用您的图标替换它怎么办。希望这对某人有所帮助。

      DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
      Toolbar mToolbar = (Toolbar)findViewById(R.id.toolbar);
      ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
                  R.string.navigation_drawer_open, R.string.navigation_drawer_close);
      
      mToggle.setDrawerIndicatorEnabled(false); // this is very important don't miss it
      mToggle.setHomeAsUpIndicator(R.drawable.ic_action_logo); // here your logo
      

      当您启用抽屉指示器时,您会失去点击切换的监听器,这就是您必须自己处理它的原因。

      mToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
      
              @Override
              public void onClick(View view) {
                    mDrawerLayout.openDrawer(GravityCompat.START)
              }
      });
      

      这就是快乐的编码:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-10
        • 2015-05-11
        • 2019-03-11
        • 1970-01-01
        相关资源
        最近更新 更多