菜单未显示在工具栏上
Menu not showing on Toolbar
我按照本指南 (https://developer.android.com/training/appbar/setting-up.html) 为我的 activity 设置了一个操作栏,但我无法显示菜单按钮(右上角的 3 条水平线)。
我正在扩展 AppCompatActivity 并使用正确显示在我的操作栏下方的 GoogleMap 片段。
在我设置的清单中:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
按照向导的指示。
菜单资源文件
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="always" />
</menu>
工具栏xml
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:title="@string/toolbar_title"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(LOG_TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//Toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
myToolbar.inflateMenu(R.menu.menu_activity_maps);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle("yoyo");
//Obtain the SupportMapFragment and call onMapReady when it is ready to be used
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
如何显示菜单?
你很接近,但你错过了那个页面上的一个 link:
https://developer.android.com/training/appbar/actions.html
基本上您只是定义了工具栏,但您还必须定义其上的所有项目以及单击时应执行的操作。
"drawable"是使用的图片。这个是心脏。
android:icon="@drawable/ic_favorite_black_48dp"
您可以通过 Android Studio 创建自己的可绘制对象,方法是转到文件->新建->图像资源,然后切换到 "Action Bar and Tab Icons"。有很多可供选择,包括您感兴趣的垂直点。
我按照本指南 (https://developer.android.com/training/appbar/setting-up.html) 为我的 activity 设置了一个操作栏,但我无法显示菜单按钮(右上角的 3 条水平线)。
我正在扩展 AppCompatActivity 并使用正确显示在我的操作栏下方的 GoogleMap 片段。
在我设置的清单中:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
按照向导的指示。
菜单资源文件
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="always" />
</menu>
工具栏xml
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:title="@string/toolbar_title"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(LOG_TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//Toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
myToolbar.inflateMenu(R.menu.menu_activity_maps);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle("yoyo");
//Obtain the SupportMapFragment and call onMapReady when it is ready to be used
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
如何显示菜单?
你很接近,但你错过了那个页面上的一个 link:
https://developer.android.com/training/appbar/actions.html
基本上您只是定义了工具栏,但您还必须定义其上的所有项目以及单击时应执行的操作。
"drawable"是使用的图片。这个是心脏。
android:icon="@drawable/ic_favorite_black_48dp"
您可以通过 Android Studio 创建自己的可绘制对象,方法是转到文件->新建->图像资源,然后切换到 "Action Bar and Tab Icons"。有很多可供选择,包括您感兴趣的垂直点。