如何在 Appbar 上显示项目
How to show items on Appbar
正在尝试在工具栏上创建项目,但它们没有显示出来。我已经在下面发布了所有必要的代码。我的工具栏显示正确,但我的项目没有显示,我不知道为什么。任何帮助将不胜感激
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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/left"
android:title="@string/app_name"
android:icon="@drawable/ic_left"
app:showAsAction="always"/>
<item
android:id="@+id/right"
android:title="@string/app_name"
android:icon="@drawable/ic_right"
app:showAsAction="ifRoom"/>
</menu>
App_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/readerBar"
android:background="@color/colorPrimaryDark"
app:title="@string/app_name"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
</androidx.appcompat.widget.Toolbar>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
layout="@layout/app_bar"
android:id="@+id/include" android:layout_height="wrap_content" android:layout_width="match_parent"/>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.readerBar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
这是我得到的结果:
您的代码 + 菜单设置看起来不错,我认为菜单项未显示,因为这部分:setSupportActionBar(toolbar);
失败(工具栏 == null);因为它在包含 ID 的布局中。
当您删除 android:id="@+id/include"
时,工具栏应该正确设置并显示项目。
Appbar
有 google 设置的限制,这使得它很难工作。
所以我有另一个建议给你,
您可以将 Appbar
替换为 RelativeLayout
或您想要的任何父布局。
更好,因为您可以按照自己的方式自定义它。
例如,您可以通过 imageView
添加您的图标,然后为它们添加 ser 侦听器并控制它们。
如果您希望您的父级高度与 Appbar
相同,请像这样设置您的布局高度。
android:layout_height="?android:attr/actionBarSize"
正在尝试在工具栏上创建项目,但它们没有显示出来。我已经在下面发布了所有必要的代码。我的工具栏显示正确,但我的项目没有显示,我不知道为什么。任何帮助将不胜感激
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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/left"
android:title="@string/app_name"
android:icon="@drawable/ic_left"
app:showAsAction="always"/>
<item
android:id="@+id/right"
android:title="@string/app_name"
android:icon="@drawable/ic_right"
app:showAsAction="ifRoom"/>
</menu>
App_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/readerBar"
android:background="@color/colorPrimaryDark"
app:title="@string/app_name"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
</androidx.appcompat.widget.Toolbar>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
layout="@layout/app_bar"
android:id="@+id/include" android:layout_height="wrap_content" android:layout_width="match_parent"/>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.readerBar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
这是我得到的结果:
您的代码 + 菜单设置看起来不错,我认为菜单项未显示,因为这部分:setSupportActionBar(toolbar);
失败(工具栏 == null);因为它在包含 ID 的布局中。
当您删除 android:id="@+id/include"
时,工具栏应该正确设置并显示项目。
Appbar
有 google 设置的限制,这使得它很难工作。
所以我有另一个建议给你,
您可以将 Appbar
替换为 RelativeLayout
或您想要的任何父布局。
更好,因为您可以按照自己的方式自定义它。
例如,您可以通过 imageView
添加您的图标,然后为它们添加 ser 侦听器并控制它们。
如果您希望您的父级高度与 Appbar
相同,请像这样设置您的布局高度。
android:layout_height="?android:attr/actionBarSize"