Android 将操作栏和选项菜单添加到“活动”
我正在为我的应用程序使用示例。此示例没有操作栏 这是活动的布局,我想在其中添加带有选项菜单的操作栏:Android 将操作栏和选项菜单添加到“活动”,android,android-actionbar,android-menu,Android,Android Actionbar,Android Menu,我正在为我的应用程序使用示例。此示例没有操作栏 这是活动的布局,我想在其中添加带有选项菜单的操作栏: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> &
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_gravity="bottom"
android:background="#000"
tools:context="com.example.android.camera2basic.CameraActivity" />
</LinearLayout>
因此,LinearLayout
和它内部的FrameLayout
作为片段的容器。在主要活动中,我实现了创建选项菜单上的oncreateoptions
,并添加了setSupportActionBar((工具栏)findViewById(R.id.my_工具栏))代码>但是菜单中的actionbar没有出现
如何将带有菜单的操作栏添加到此活动?在菜单文件夹中创建一个main_menu.xml
<item
android:id="@+id/menu1"
android:title="Option 1" />
<item
android:id="@+id/menu2"
android:title="Optiion 2" />
将活动主题设置为NoActionBar,并在ActivityOnCreate方法中添加这两行代码
在布局中添加工具栏
ToolBar toolbar = findViewById ();
setSupportActionBar(toolbar);
问题是我无法在活动中显示ActionBar,我知道如何制作菜单。您是否正在扩展AppCompatActivity并以样式使用Theme.AppCompat.Light.DarkActionBar?
ToolBar toolbar = findViewById ();
setSupportActionBar(toolbar);