工具栏中未显示菜单图标

3

我尝试在工具栏中添加菜单。我写了一些代码,但出现了非常奇怪的情况。我看不到菜单图像在我的工具栏右侧,但当我点击它(我的意思是在工具栏右侧)时,菜单会显示。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.unipay.business.activities.MainActivity">


    <item
        android:id="@+id/transaction_all"
        android:orderInCategory="100"
        android:title="@string/history_all"
        app:showAsAction="never" />
    <item
        android:id="@+id/transaction_Income"
        android:orderInCategory="100"
        android:title="@string/history_income"
        app:showAsAction="never" />
    <item
        android:id="@+id/transaction_Outcome"
        android:orderInCategory="100"
        android:title="@string/u_history_outcome"
        app:showAsAction="never" />
    <item
        android:id="@+id/transaction_advanced_search"
        android:orderInCategory="100"
        android:title="@string/u_history_advance"
        app:showAsAction="never" />

我尝试创建像这样的菜单

enter image description here

这是我的自定义工具栏布局XML代码

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/u_common_header_height"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ToolbarTheme"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextAppearance="@style/Toolbar.TitleText">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/u_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/u_sign_open_menu_margin"
        android:padding="@dimen/u_sign_open_menu_margin"
        android:src="@mipmap/ic_arrow_back_white" />


    <ImageView
        android:id="@+id/logo_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@mipmap/logo_header"
        android:visibility="gone" />

    <TextView
        android:id="@+id/header_tittle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="72dp"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="@dimen/u_common_text_size" />

</RelativeLayout>

我是这样初始化我的工具栏的

 toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

这是一种工具栏样式

  <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <!-- android:textColorPrimary is the color of the title text in the Toolbar  -->
    <item name="android:textColorPrimary">@android:color/holo_blue_light</item>
    <!-- actionMenuTextColor is the color of the text of action (menu) items  -->
    <item name="actionMenuTextColor">@android:color/holo_green_light</item>
    <!-- Tints the input fields like checkboxes and text fields -->
    <item name="colorAccent">@color/colorPrimary</item>
    <!-- Applies to views in their normal state. -->
    <item name="colorControlNormal">@color/colorPrimary</item>

    <item name="colorControlActivated">@color/colorPrimary</item>

    <item name="colorControlHighlight">@color/colorPrimary</item>
</style>


<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">21sp</item>
    <item name="android:textStyle">italic</item>
</style>

有人能告诉我我做错了什么吗?就像我说的,菜单在工具栏中可以正常工作,但我看不到菜单图片。谢谢。


1
请查看此链接:https://dev59.com/WmIj5IYBdhLWcg3w8JNZ - Shiv Buyya
2个回答

3
try this

import android.support.v7.view.menu.MenuBuilder;

@SuppressLint("RestrictedApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (menu instanceof MenuBuilder) {
        ((MenuBuilder) menu).setOptionalIconsVisible(true);
    }
    getMenuInflater().inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

0
app:showAsAction="never"

修改为app:showAsAction="always"或者"ifRoom"


我更新了我的问题,请查看。我也尝试了你的版本,但我想创建下拉菜单。@Sangram Haladkar - Georgo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接