如何更改android中的汉堡包图标和溢出图标?

时间:2016-02-15 05:31:31

标签: android menu android-actionbar styles

我想更改汉堡包图标和溢出图标(仅限颜色),因为我已经制作了与我想要的颜色相同的mipmap资源。 (由于我的ActionBars背景为白色,我希望我的溢出菜单和汉堡包颜色为蓝色)。我正在给ActionBar的风格,一切都按预期工作只有汉堡包和溢出图标不会从默认的android更改。那我怎么能改变这两个图标呢?

这是我给ActionBar的风格。

 <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat">

<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>
<item name="dropDownListViewStyle">@style/PopupMenuListView</item>

<item name="actionBarDivider">@null</item>


<item name="android:homeAsUpIndicator">@drawable/img_menu</item>
<item name="homeAsUpIndicator">@drawable/img_menu</item>

<!-- OverFlow Menu Text Color -->
<item name="android:textColor">@color/white</item>
</style>
<!-- Set Custom Icon for Overflow Button in ActionBar -->
<style name="OverflowButton"  parent="@style/Widget.AppCompat.ActionButton.Overflow">
    <item name="android:src">@drawable/img_overflow</item>
</style>

<!-- ActionBar styles for Text Style, Background-->
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@color/white</item>

    <item name="colorControlNormal">@color/lightBlue</item>
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    <item name="titleTextStyle">@style/MyActionBarTitleText</item>
</style>

<!-- ActionBar styles for Text Style -->
<style name="MyActionBarTitleText"  parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/lightBlue</item>
    <item name="android:actionMenuTextColor">@color/dark_gray</item>
</style>

<!-- OverFlow menu Styles -->
<style name="PopupMenuListView" parent="@style/Widget.AppCompat.Light.ListView.DropDown">
    <item name="android:divider">@color/white</item>

    <item name="android:dividerHeight">1dp</item>

    <item name="android:background">@color/transparent_color</item>
</style>

这是我的活动代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    txt_title = (TextView)mToolbar.findViewById(R.id.txt_title);
    Utilities.getInstance().setBoldFont(txt_title,this);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    drawerFragment.setDrawerListener(this);

    // display the first navigation drawer view on app launch

    if (AppConstants.isItemVisible.equals("5"))
    displayView(5);

    else displayView(1);
}

2 个答案:

答案 0 :(得分:0)

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/black</item>
</style>

从上面的代码

中查看此行
<item name="color">@android:color/black</item>

将颜色更改为您想要的颜色

答案 1 :(得分:0)

这应该照顾你的汉堡包图标 -

Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_menu, null);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, Color.BLUE);
actionBar.setHomeAsUpIndicator(drawable);

现在为你的溢出菜单 -

<resources>

    <style name="my_app_action_bar_theme" parent="android:Theme.Holo.Light">
        <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>

    </style>

    <!-- The beef: background color for Action Bar overflow menu -->

     <style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel</item>
        <item name="android:background">@drawable/menu_dropdown_panel</item>
     </style>
</resources>

menu_dropdown_panel。将此项放在您的drawable文件夹中。 -

<corners android:radius="3.0dip" />

<stroke
    android:width="1.0dip"
    android:color="@color/transparent" />

<solid android:color="@color/blue" />

android_manifest

<application
    android:theme="@style/my_app_action_bar_theme" 
    ... >

<强>更新 您还需要在xml中为工具栏指定这些 -

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/hotspot_blue"
        app:theme="@style/my_action_bar_theme"
        app:popupTheme="@style/MyApp.PopupMenu"
        />

并且不要忘记包含此

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.app_name));
setSupportActionBar(toolbar);