Change Background from the ActionBar menu

1

Hello, is there any way to change in XML the background color of the options menu that appears in ActionBar ?

    
asked by anonymous 05.03.2015 / 17:52

1 answer

0

You have two options:

1) If you're using Toolbar (if you're not using it, I recommend), you can insert components into your toolbar and tinker as you want. For example:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/SeuTema"
    android:gravity="center_horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageButton
        android:id="@+id/img_exemplo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/sua_cor"
        android:src="@drawable/seu_icone" />

</android.support.v7.widget.Toolbar>

2) If you are not using Toolbar , there is a way to "hack" it according to this post , and I will summarize it for you:

The menu items have various styles, manners, appearances, and so on. devices from different manufacturers. There is no documented way to do this. There are some options listed in the post, but they are far from ideal options for solving this problem, and it does not work on all devices.

If you really need to have a background for your menu items, I suggest that you study the implementation of Toolbar in your project so you do not have to deal with any unwanted "surprises". If this is the case, follow the official documentation on how to implement them and there are some articles that can help you a lot.

    
05.03.2015 / 18:53