Я пытаюсь показать MenuItem
, когда fab
приложения скрыто в BottomAppBar
, и сделать его снова видимым, когда я покажу fab
. Однако когда я скрываю fab
и делаю видимым MenuItem
, первый мигает быстро (появляется, исчезает, а затем снова появляется и остается). В зависимости от моего Manifest
я добавил: implementation 'com.google.android.material:material:1.1.0'
.
Для этого примера я создал кнопку, которая показывает / скрывает fab
.
Мой MainActivity
выглядит так:
public class MainActivity extends AppCompatActivity {
private FloatingActionButton fab;
private MenuItem menuItm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomAppBar toolbar = findViewById(R.id.bottom_app_bar);
setSupportActionBar(toolbar);
fab = findViewById(R.id.fab);
}
public void showHideFab(View view) {
if (fab.isOrWillBeHidden()) {
fab.show();
menuItm.setVisible(false);
} else {
fab.hide();
menuItm.setVisible(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menuItm = menu.findItem(R.id.menu_itm);
return super.onPrepareOptionsMenu(menu);
}
}
Файл макета 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">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coord"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="@color/colorPrimary"
app:fabAlignmentMode="end" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottom_app_bar"
app:srcCompat="@android:drawable/ic_dialog_alert" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:onClick="showHideFab"
android:text="Show/Hide Fab" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Мой menu/menu_main.xml
:
<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="com.teka.loulis.print.MainActivity">
<item
android:id="@+id/menu_itm"
android:icon="@android:drawable/ic_dialog_alert"
android:visible="false"
app:showAsAction="always" />
</menu>
Мой styles.xml
:
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorSecondary">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
и объявление MainActivity
в Manifest
:
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
В версии 1.0.0 этой проблемы не было. Я делаю что-то не так, или это ошибка / особенность библиотеки? Спасибо!
РЕДАКТИРОВАТЬ: Я заметил, что когда fab
выровнен по центру (удалив app:fabAlignmentMode="end"
из файла макета activity_main
), эта проблема исчезла. При выравнивании в конце возникают другие проблемы, например, если я сделаю fab
и пункт меню видимым одновременно, элемент меню иногда рисуется поверх fab
! Для меня это больше похоже на ошибку.