有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java工具栏中的溢出菜单图标

我正在使用appCompat v7:24.2.1,我刚刚学习了一个关于材料的简单教程。我只是不知道如何在创建的工具栏的右侧中获得溢出菜单

The tutorial that i used..

自定义应用程序工具栏

<安卓.support.v7.widget.Toolbar xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="#F44336"
>

梅因。xml

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/activity_main"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.ud.materialdesigntest.MainActivity">

<!-- Was inside the RelativeLayout Tag
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
-->


<include
    安卓:id="@+id/app_bar"
    layout="@layout/app_bar" />

<TextView
    安卓:layout_below="@+id/app_bar"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="Hello World"
    安卓:id="@+id/textViewHelloWorld" />

梅因。爪哇

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;


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


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

}

}


共 (1) 个答案

  1. # 1 楼答案

    尝试使用以下代码:

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.my_icon);
    

    要更改溢出图标,可以定义如下样式:

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light">
        <item name="actionOverflowButtonStyle">@style/OverFlow</item>
    </style>
    
    <style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
        <item name="android:src">@drawable/my_overflow_menu</item>
    </style>
    

    无论如何,更改标准图标(如溢出菜单)可能不是一个好主意

    如果要更改图标的颜色,可以使用:

    <android.support.v7.widget.Toolbar
      app:theme="@style/ThemeToolbar" />
    
    
    <style name="ThemeToolbar" parent="Theme.AppCompat.Light">
    
       <!  navigation icon color  >
       <item name="colorControlNormal">@color/my_color</item>
    
        <!  color of the menu overflow icon  >
        <item name="android:textColorSecondary">@color/my_color</item>
    </style>