向工具栏添加菜单

时间:2020-09-23 13:28:35

标签: android toolbar

试图在菜单栏上添加一个始终显示在工具栏右侧的菜单项

菜单文件

<item android:id="@+id/menu_edit"
    android:title="@string/edit"
    android:icon="@drawable/ic_baseline_edit_24"
    app:showAsAction="ifRoom"
   />

xml文件

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context=".Profile">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/profileAppBar"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/profileCollapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="@color/mediumBlue"
        app:layout_scrollFlags="exitUntilCollapsed|scroll"

        >


        <ImageView
            android:id="@+id/profileFullImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/abstract_dark_blue_polygonal_background_1035_9700"
            android:fitsSystemWindows="true"
            android:transitionName="profileImage"
            app:layout_collapseMode="parallax" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shape_background" />

        <androidx.appcompat.widget.Toolbar

            app:menu="@menu/profile_toolbr"
            android:id="@+id/profileToolBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

         >

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.CollapsingToolbarLayout>


</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

java.class

public class Profile extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    toolbar = findViewById(R.id.profileToolBar);
    collapsingToolbarLayout = findViewById(R.id.profileCollapsingToolbarLayout);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);




}

问题是它没有显示,当我添加它时仅显示为三个点

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = new MenuInflater(Profile.this);
    menuInflater.inflate(R.menu.profile_toolbr,menu);
    return true ;
}

如何添加它以在工具栏上显示为图标 我尝试使用app:showAsAction =“ always”,但不变没有改变

1 个答案:

答案 0 :(得分:2)

您不应使用MenuInflater,因为它将创建新菜单。菜单由活动本身处理。因此,您无需使用新的MenuInflater。因此,您必须将其删除并使用getMenuInflater

为此替换您的onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.main_menu, menu);
   return super.onCreateOptionsMenu(menu);
}