有没有一种简单的方法可以将按钮添加到子活动的操作栏?

我正在尝试将按钮添加到 android studio 应用程序中的操作栏到子活动,但我只设法将它添加到主要活动。有人知道我该怎么做吗?

我希望“确定”按钮位于我用橙色绘制的位置。

带有操作栏的主要活动。

http://img4.mukewang.com/640947fb0001718105240400.jpg

带操作栏的儿童活动

http://img3.mukewang.com/6409480a00013af305210359.jpg

红颜莎娜
浏览 105回答 3
3回答

蓝山帝景

您应该为您的活动创建一个菜单:里面res&nbsp;->&nbsp;menu选择new&nbsp;->&nbsp;Menu资源文件在您的新菜单 xml 文件中写入以下内容:<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto">&nbsp; &nbsp; <item&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/menu_action_ok"&nbsp; &nbsp; &nbsp; &nbsp; android:title="Ok"&nbsp; &nbsp; &nbsp; &nbsp; app:showAsAction="ifRoom" /></menu>基本上,您正在创建一个菜单,其中包含一个名为“确定”的选项,但如果需要,您可以有更多选项。如果需要,您还可以自定义此选项的视图:app:actionLayout="@layout/filter_ka"在您的父活动中:@Overridepublic boolean onCreateOptionsMenu(Menu menu) {&nbsp; &nbsp; // Inflate the menu.&nbsp; &nbsp; getMenuInflater().inflate(R.menu.menu_test, menu);&nbsp; &nbsp; return true;}R.菜单。menu_test是菜单文件的名称。最后要接收菜单选项的点击,您应该重写以下函数:@Overridepublic boolean onOptionsItemSelected(MenuItem item) {&nbsp; &nbsp; // Handle action bar item clicks here. The action bar will&nbsp; &nbsp; // automatically handle clicks on the Home/Up button, so long&nbsp; &nbsp; // as you specify a parent activity in AndroidManifest.xml.&nbsp; &nbsp; int id = item.getItemId();&nbsp; &nbsp; if (id == R.id.menu_action_ok) {&nbsp; &nbsp; &nbsp; &nbsp; //Your code&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; return super.onOptionsItemSelected(item);}现在你应该有一个像这样的菜单:

四季花海

您必须为此创建一个菜单。假设你创建add_menu<?xml version="1.0" encoding="utf-8"?><menu&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto">&nbsp; &nbsp; <item&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/action_add"&nbsp; &nbsp; &nbsp; &nbsp; android:title="ADD"&nbsp; &nbsp; &nbsp; &nbsp; app:showAsAction="ifRoom"/></menu>创建菜单后,使用以下命令将其扩展到您的子活动。&nbsp; &nbsp;@Override&nbsp; &nbsp; public boolean onCreateOptionsMenu(Menu menu) {&nbsp; &nbsp; &nbsp; &nbsp; getMenuInflater().inflate(R.menu.add_menu, menu);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }

幕布斯7119047

您不必使用操作栏。您只能使用您想要的背景颜色的视图。在您将按钮设置在彩色区域上方之后。在 Android Studio 中:价值观>风格用这个 :<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">代替&nbsp;<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">回到你的布局:// Change your background with LinearLayout's background propoerty&nbsp; &nbsp;<LinearLayout&nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp;android:orientation="horizontal"&nbsp; &nbsp; &nbsp; &nbsp;android:background="#FFA908"&nbsp; &nbsp; &nbsp; &nbsp;>&nbsp; &nbsp; &nbsp; &nbsp;// You can put here whatever you want. İf you wish image or button.&nbsp; &nbsp; &nbsp; &nbsp;<Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:text="Example"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_marginLeft="200dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/>&nbsp; &nbsp; &nbsp; &nbsp;<Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:text="Example"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/>&nbsp; &nbsp;</LinearLayout>İf 你可以像这样使用。您不需要使用操作栏。你可以轻松地创造任何你想要的东西。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java