如何在安卓中将菜单设置为工具栏
2022-08-31 08:25:28
我想使用而不是 ,但不在工具栏中显示菜单!!!我想要设置菜单,如 刷新 或 设置 按钮 在 .ToolBar
ActionBar
ActionBar
工具栏.xml代码 :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:navigationContentDescription="@string/abc_action_bar_up_description"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Main Page"
android:gravity="center"/>
主页.java代码:
public class MainPage extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
toolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("Main Page");
}
toolbar.setSubtitle("Test Subtitle");
toolbar.inflateMenu(R.menu.main_menu);
}
}
main_menu.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/menu_main_setting"
android:icon="@drawable/ic_settings"
android:orderInCategory="100"
app:showAsAction="always"
android:actionLayout="@layout/toolbar"
android:title="Setting" />
<item
android:id="@+id/menu_main_setting2"
android:icon="@drawable/ic_settings"
android:orderInCategory="200"
app:showAsAction="always"
android:actionLayout="@layout/toolbar"
android:title="Setting" />
</menu>
如何解决此问题并在 中显示菜单?谢谢大家亲爱的<3Toolbar