【问题标题】:How to change the menu icon of a Toolbar in AppCompat [duplicate]如何在 AppCompat 中更改工具栏的菜单图标 [重复]
【发布时间】:2014-12-23 15:12:58
【问题描述】:

我正在尝试做一些非常简单的事情,但我没有在互联网上找到任何关于如何解决它的东西,也没有在 stackoverflow 上找到任何东西。我想更改菜单图标颜色以匹配我的其他图标。我该怎么做?。 Toolbar 的 setNavigationIcon 和 setLogo 方法不起作用。这是我当前Toolbar的截图:

这是我在onCreate 方法中执行的代码。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_naviation_drawer);
        mItems = getResources().getStringArray(R.array.nav_drawer_items);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawer.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START);
        //For api-21 compatibility
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mDrawerToggle= new ActionBarDrawerToggle(this, mDrawer, mToolbar, R.string.app_name, R.string.app_name);
            mDrawer.setDrawerListener(mDrawerToggle);
            mToolbar.setNavigationIcon(R.drawable.ic_menu); //Doest work (is the white icon)
            mToolbar.setLogo(R.drawable.ic_menu); //Doest work (is the white icon)
        }
        mToolbar.inflateMenu(R.menu.startup_navigation_drawer);
        mContentViewAlreadySet = Boolean.TRUE;

        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mItems));
        // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    }

我不在乎当我点击它时是否结束覆盖菜单图标动画,我只是希望它与我的其他图标颜色匹配。

更新:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light">
        <!--<item name="android:buttonStyle">@style/AppTheme.Buttons</item>-->
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="isFloatingWindow">false</item>
        <!-- colorPrimary is used, for instance, for the default ActionBar
       (but not Toolbar) background.
        We specify the same color for the toolbar background in
        toolbar.xml.. -->
        <item name="colorPrimary">@color/colorPrimary</item>

        <!-- colorPrimaryDark is used for the status bar (with the
             battery, clock, etc). -->
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

        <!-- colorAccent is used as the default value for
             colorControlActivated which is used to tint widgets. -->
        <item name="colorAccent">@color/colorSecondary</item>
        <item name="android:textColorPrimary">@color/colorSecondary</item>
    </style>

  <style name="AppTheme" parent="@style/_AppTheme"/>

  <style name="_AppTheme" parent="AppTheme.Base">

    <item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>

    <item name="android:textColorHighlight">#9980cbc4</item>
    <item name="android:textSelectHandleLeft">@drawable/apptheme_text_select_handle_left</item>
    <item name="android:textSelectHandleRight">@drawable/apptheme_text_select_handle_right</item>
    <item name="android:textSelectHandle">@drawable/apptheme_text_select_handle_middle</item>

    <item name="android:autoCompleteTextViewStyle">@style/AutoCompleteTextViewAppTheme</item>

    <item name="android:listChoiceIndicatorMultiple">@drawable/apptheme_btn_check_holo_light</item>

    <item name="android:listChoiceIndicatorSingle">@drawable/apptheme_btn_radio_holo_light</item>

    <item name="android:buttonStyle">@style/ButtonAppTheme</item>

    <item name="android:imageButtonStyle">@style/ImageButtonAppTheme</item>

    <item name="android:dropDownSpinnerStyle">@style/SpinnerAppTheme</item>

    <item name="android:progressBarStyleHorizontal">@style/ProgressBarAppTheme</item>

    <item name="android:seekBarStyle">@style/SeekBarAppTheme</item>

    <item name="android:ratingBarStyleIndicator">@style/RatingBarBigAppTheme</item>

    <item name="android:ratingBarStyleSmall">@style/RatingBarSmallAppTheme</item>

    <item name="android:buttonStyleToggle">@style/ToggleAppTheme</item>

    <item name="android:listChoiceBackgroundIndicator">@drawable/apptheme_list_selector_holo_light</item>

    <item name="android:activatedBackgroundIndicator">@drawable/apptheme_activated_background_holo_light</item>

    <item name="android:fastScrollThumbDrawable">@drawable/apptheme_fastscroll_thumb_holo</item>

  </style>

</resources>

【问题讨论】:

  • tycz 成功了!谢谢!。我会将这个答案标记为重复。

标签: android android-appcompat android-toolbar


【解决方案1】:

如果你只想匹配颜色:

在您的应用主题中设置

<item name="colorAccent">@color/whatever</item>

【讨论】:

  • 我的 style.xml 中已经有了该属性。我更新了我的答案。并且 colorAccent 不是屏幕截图中显示的颜色。也许我的风格有错误的父母?
猜你喜欢
  • 2015-05-16
  • 2015-02-04
  • 1970-01-01
  • 2015-03-15
  • 2015-01-23
  • 1970-01-01
  • 2014-12-20
  • 2017-03-02
相关资源
最近更新 更多