Android 上未显示溢出和其他菜单项
Overflow and other menu items not displayed on Android
我目前正在尝试添加一个工具栏作为操作栏,但我无法显示除应用程序名称之外的任何内容。我正在从 appcompat 添加工具栏并创建了菜单 xml 文件。为了 link 这两个,我需要采取必要的步骤吗?
这是我的 activity xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".canvas.CanvasActivity"
>
<com.alastech.andyluo.crowdshout_androidapp.canvas.CanvasTest
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view"
android:layout_gravity="center" />
<android.support.v7.widget.Toolbar
android:id="@+id/canvas_toolbar"
android:layout_width="match_parent"
android:elevation="0dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</FrameLayout>
我确信工具栏已正确添加到布局中,因为我已经能够更改其大小并且在它运行时已正确反映出来。
这是菜单的xml:
<?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_new_shout"
android:icon="@drawable/ic_add_black_48dp"
android:title="@string/action_new_shout"
app:showAsAction="always"
/>
<item
android:id="@+id/action_menu"
android:title="@string/action_menu"
app:showAsAction="never"
/>
</menu>
这是操作栏中唯一显示的内容:
需要说明的是,我几乎完全按照本教程进行操作:
http://developer.android.com/training/appbar/index.html
感谢任何帮助,谢谢!
在你里面添加下面的方法activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// use sample_actions or wheever your xml for the menufile name below
getMenuInflater().inflate(R.menu.sample_actions, menu);
return true;
}
我目前正在尝试添加一个工具栏作为操作栏,但我无法显示除应用程序名称之外的任何内容。我正在从 appcompat 添加工具栏并创建了菜单 xml 文件。为了 link 这两个,我需要采取必要的步骤吗?
这是我的 activity xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".canvas.CanvasActivity"
>
<com.alastech.andyluo.crowdshout_androidapp.canvas.CanvasTest
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view"
android:layout_gravity="center" />
<android.support.v7.widget.Toolbar
android:id="@+id/canvas_toolbar"
android:layout_width="match_parent"
android:elevation="0dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</FrameLayout>
我确信工具栏已正确添加到布局中,因为我已经能够更改其大小并且在它运行时已正确反映出来。
这是菜单的xml:
<?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_new_shout"
android:icon="@drawable/ic_add_black_48dp"
android:title="@string/action_new_shout"
app:showAsAction="always"
/>
<item
android:id="@+id/action_menu"
android:title="@string/action_menu"
app:showAsAction="never"
/>
</menu>
这是操作栏中唯一显示的内容:
需要说明的是,我几乎完全按照本教程进行操作: http://developer.android.com/training/appbar/index.html
感谢任何帮助,谢谢!
在你里面添加下面的方法activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// use sample_actions or wheever your xml for the menufile name below
getMenuInflater().inflate(R.menu.sample_actions, menu);
return true;
}