Я хочу сделать уведомление в верхней части моего приложения с анимацией появления / скрытия. Для этого я создал FrameLayout (flNotification
) с вложенным TextView (tvNotificationText
)
Анимация работает приемлемо, но у меня проблемы с z-порядком - FrameLayout отображается за кнопкой
Скриншот ошибки :
![screenshot](https://i.stack.imgur.com/2Eb3K.jpg)
Что мне нужно изменить, так это то, что flNotification отображается в верхней части кнопки.
Мин. SDK: 17
Показать способ уведомления
public void showNotification(String message) {
tvNotificationText.setText(message);
flNotification.bringToFront(); // doesn't help
clRoot.invalidate(); // doesn't help
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.notification_animation);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
flNotification.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
flNotification.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
flNotification.startAnimation(animation);
}
Макет XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:id="@+id/clRoot"
tools:context=".activities.MainActivity">
[...]
<Button
android:id="@+id/btnNameAdd"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:text="+"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="@+id/etAge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/etName" />
[...]
[...]
[...]
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/flNotification"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"
android:background="@color/errorNotificationBackground"
android:clipChildren="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvNotificationText"
android:layout_gravity="center"
tools:text="Lorem ipsum dolor sit amet"
android:textColor="@color/errorNotificationForeground"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp">
</TextView>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Файл анимации
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="-100%p"
android:toYDelta="0%p"
android:duration="750"/>
<translate
android:startOffset="2500"
android:fromYDelta="0%p"
android:toYDelta="-100%p"
android:duration="750"/>
</set>