显示快餐栏时如何上推现有视图? [英] How to push up an existing view when snackbar is displayed?
问题描述
这是我尝试在显示snackbar
时向上推动"NEXT"按钮的方法:
This is my attempt to push the "NEXT" button upwards when the snackbar
is visible:
如您所见,它无法按计划进行.看起来好像textview
被向上推并在RelativeLayout
后面,然后在snackbar
消失时重新出现.相反,我想要的是textview
出现被推上(因此它在snackbar
之上),然后在snackbar
消失时又向下出现.我还创建了一个小的github存储库来演示这一点:
As you can see, it doesn't work as planned. It appears as if the textview
was pushed up and behind the RelativeLayout
and then reappears when the snackbar
disappears. Instead, what I want is the textview
to appear pushed up (so it is above the snackbar
) and then come back down when the snackbar
disappears. I have also created a small github repo to demonstrate this:
https://github.com/Winghin2517/TestFabMotion
这是我在"NEXT"中用来隐藏自身的CoordinatorLayout.Behavior
.当snackbar
出现时,行为被推升到FAB行为:
Here is the CoordinatorLayout.Behavior
I use for the "NEXT" to hide itself. The Behavior is taken from the FAB behavior when it gets pushed up when the snackbar
appears:
public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<TextView> {
public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, TextView child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, TextView child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
}
我已使用以下xml尝试过此操作:
I have tried this using this xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Here!"
android:textAllCaps="true"
android:textStyle="bold"
android:background="@color/colorAccent"
android:id="@+id/click_here"
android:textSize="22sp"
android:layout_marginBottom="12dp"
android:gravity="center_horizontal"
android:paddingTop="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/click_here"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/next"
android:text="Next"
android:layout_alignParentBottom="true"
android:background="@color/colorAccent"
android:clickable="true"
android:gravity="center_horizontal"
android:padding="16dp"
android:textAllCaps="true"
android:textStyle="bold"
app:layout_behavior="fabmotiontest.com.fabtest.FloatingActionButtonBehavior" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
我也尝试过使用此xml:
I have also tried using this xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Here!"
android:textAllCaps="true"
android:textStyle="bold"
android:background="@color/colorAccent"
android:id="@+id/click_here"
android:textSize="22sp"
android:layout_marginBottom="12dp"
android:gravity="center_horizontal"
android:paddingTop="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/click_here"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorlayout"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/next"
android:text="Next"
android:background="@color/colorAccent"
android:clickable="true"
android:gravity="center_horizontal"
android:padding="16dp"
android:textAllCaps="true"
android:textStyle="bold"
app:layout_behavior="fabmotiontest.com.fabtest.FloatingActionButtonBehavior" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
它似乎不起作用.有人知道我如何使它工作吗?
It just doesn't seem to work. Does anyone know how I can get it working?
推荐答案
在测试了几种解决方案之后,我遇到了一个有效的xml布局.
After testing several solutions, I came across one xml layout that works.
我之前和下面所尝试的主要区别是coordinatorlayout
的高度为"match_parent
",这意味着它覆盖了其下方的视图,但是由于coordinatorlayout
是透明的,因此您看不到现在,当snackbar
从底部向上推时,它将有足够的空间将textview
推到snackbar
上方:
The main difference between what I had tried before and the below is that the coordinatorlayout
height is "match_parent
" which means it overlays the views below it but because the coordinatorlayout
is transparent, you don't see it and when the snackbar
pushes up from the bottom, it will now have enough room to push the textview
above the snackbar
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Here!"
android:textAllCaps="true"
android:textStyle="bold"
android:background="@color/colorAccent"
android:id="@+id/click_here"
android:textSize="22sp"
android:layout_marginBottom="12dp"
android:gravity="center_horizontal"
android:paddingTop="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp" />
<ImageButton
android:id="@+id/btnComments"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_below="@+id/click_here"
android:layout_centerHorizontal="true"
android:background="@drawable/shadow_bg"
android:src="@mipmap/ic_launcher" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/next"
android:text="Next"
android:background="@color/colorAccent"
android:clickable="true"
android:gravity="center_horizontal"
android:padding="16dp"
android:textAllCaps="true"
android:textStyle="bold"
android:layout_gravity="center_horizontal|bottom"
app:layout_behavior="fabmotiontest.com.fabtest.FloatingActionButtonBehavior" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
这篇关于显示快餐栏时如何上推现有视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!