#android #toolbar
Вопрос:
я пытаюсь добавить один пункт меню на панель инструментов, который всегда отображается справа от панели инструментов
файл меню
lt;item android:id="@ id/menu_edit" android:title="@string/edit" android:icon="@drawable/ic_baseline_edit_24" app:showAsAction="ifRoom" /gt;
xml-файл
lt;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"gt; lt;com.google.android.material.appbar.AppBarLayout android:id="@ id/profileAppBar" android:layout_width="match_parent" android:layout_height="400dp" android:fitsSystemWindows="true"gt; lt;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" gt; lt;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" /gt; lt;LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/shape_background" /gt; lt;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" gt; lt;/androidx.appcompat.widget.Toolbargt; lt;/com.google.android.material.appbar.CollapsingToolbarLayoutgt; lt;/com.google.android.material.appbar.AppBarLayoutgt; lt;/androidx.coordinatorlayout.widget.CoordinatorLayoutgt;
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 ; }
как я могу добавить его, чтобы он отображался в виде значка на панели инструментов, я пытаюсь использовать приложение:showAsAction=»всегда», но то же самое без изменений
Ответ №1:
Вы не должны использовать MenuInflater
, так как это создаст новое меню. Меню обрабатывается самим действием. Так что вам не нужно использовать новые MenuInflater
. Поэтому вы должны удалить его и использовать getMenuInflater
.
Замените свой onCreateOptionsMenu
на этот:
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); }