Android 在Snackbar上移动cardview-协调员布局
我在屏幕底部的cardview中有一个自定义文本视图,并使用snackbar显示登录时的错误消息。现在当snackbar显示时,注册文本视图应该向上移动。我尝试过使用协调员布局,但它不起作用。这是您需要为视图实现布局行为并从布局xml文件中引用它的图像 实现自己的布局行为很简单。在您的情况下,只需在snackbar出现时设置视图的平移yAndroid 在Snackbar上移动cardview-协调员布局,android,material-design,androiddesignsupport,android-coordinatorlayout,android-snackbar,Android,Material Design,Androiddesignsupport,Android Coordinatorlayout,Android Snackbar,我在屏幕底部的cardview中有一个自定义文本视图,并使用snackbar显示登录时的错误消息。现在当snackbar显示时,注册文本视图应该向上移动。我尝试过使用协调员布局,但它不起作用。这是您需要为视图实现布局行为并从布局xml文件中引用它的图像 实现自己的布局行为很简单。在您的情况下,只需在snackbar出现时设置视图的平移y public class YourCustomBehavior extends CoordinatorLayout.Behavior<View> {
public class YourCustomBehavior extends CoordinatorLayout.Behavior<View> {
public YourCustomBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
// we only want to trigger the change
// only when the changes is from a snackbar
return dependency instanceof Snackbar.SnackbarLayout;
}
}
public类YourCustomBehavior扩展了CoordinatorLayout.Behavior{
公共YourCustomBehavior(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
公共布尔onDependentViewChanged(协调布局父项、视图子项、视图依赖项){
float translationY=Math.min(0,dependency.getTranslationY()-dependency.getHeight());
child.setTranslationY(translationY);
返回true;
}
@凌驾
公共布尔布局依赖项(协调布局父项、视图子项、视图依赖项){
//我们只想引发变化
//仅当更改来自快捷键时
返回Snackbar.Snackbar布局的依赖实例;
}
}
并将其添加到布局xml中,如下所示
<android.support.v7.widget.CardView
android:id="@+id/your_sign_up_card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
app:layout_behavior="com.your.app.YourCustomBehavior"/>
这篇文章很好地解释了一切。您需要为视图实现布局行为,并从布局xml文件中引用它 实现自己的布局行为很简单。在您的情况下,只需在snackbar出现时设置视图的平移y
public class YourCustomBehavior extends CoordinatorLayout.Behavior<View> {
public YourCustomBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
// we only want to trigger the change
// only when the changes is from a snackbar
return dependency instanceof Snackbar.SnackbarLayout;
}
}
public类YourCustomBehavior扩展了CoordinatorLayout.Behavior{
公共YourCustomBehavior(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
公共布尔onDependentViewChanged(协调布局父项、视图子项、视图依赖项){
float translationY=Math.min(0,dependency.getTranslationY()-dependency.getHeight());
child.setTranslationY(translationY);
返回true;
}
@凌驾
公共布尔布局依赖项(协调布局父项、视图子项、视图依赖项){
//我们只想引发变化
//仅当更改来自快捷键时
返回Snackbar.Snackbar布局的依赖实例;
}
}
并将其添加到布局xml中,如下所示
<android.support.v7.widget.CardView
android:id="@+id/your_sign_up_card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
app:layout_behavior="com.your.app.YourCustomBehavior"/>
这篇文章很好地解释了一切。这是我做的,以防有人注意到
activity_login.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.mypackage.CoordinatorBehavior"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/signUpCardView"
android:gravity="center">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:cardBackgroundColor="@color/light_gray"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="6dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="@+id/userNameEditText_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/userNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username_hint"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/passwordEditText_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/userNameEditText_layout">
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/userNameEditText"
android:hint="@string/password_hint"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/passwordEditText_layout"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
card_view:cardBackgroundColor="@color/blue_normal"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp">
<com.myview android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/title_activity_login"
android:textColor="@color/title_white"
android:textSize="@dimen/large_text_size"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/signUpCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
card_view:cardBackgroundColor="@color/refresh_red"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp">
<com.myview.TSCustomFontTextView
android:id="@+id/signUpButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/title_activity_sign_up"
android:textColor="@color/title_white"
android:textSize="@dimen/large_text_size"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
</RelativeLayout>
CoordinatorBehavior.java
public class CoordinatorBehavior extends CoordinatorLayout.Behavior<View> {
public CoordinatorBehavior(Context context, AttributeSet attrs) {
}
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
公共类CoordinatorBehavior扩展CoordinatorLayout.Behavior{
公共协调人行为(上下文、属性集属性){
}
公共布尔布局依赖项(协调布局父项、视图子项、视图依赖项){
返回Snackbar.Snackbar布局的依赖实例;
}
公共布尔onDependentViewChanged(协调布局父项、视图子项、视图依赖项){
float translationY=Math.min(0,dependency.getTranslationY()-dependency.getHeight());
child.setTranslationY(translationY);
返回true;
}
}这是我做的,以防有人注意
activity_login.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.mypackage.CoordinatorBehavior"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/signUpCardView"
android:gravity="center">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:cardBackgroundColor="@color/light_gray"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="6dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="@+id/userNameEditText_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/userNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username_hint"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/passwordEditText_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/userNameEditText_layout">
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/userNameEditText"
android:hint="@string/password_hint"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/passwordEditText_layout"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
card_view:cardBackgroundColor="@color/blue_normal"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp">
<com.myview android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/title_activity_login"
android:textColor="@color/title_white"
android:textSize="@dimen/large_text_size"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/signUpCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
card_view:cardBackgroundColor="@color/refresh_red"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp">
<com.myview.TSCustomFontTextView
android:id="@+id/signUpButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/title_activity_sign_up"
android:textColor="@color/title_white"
android:textSize="@dimen/large_text_size"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
</RelativeLayout>
CoordinatorBehavior.java
public class CoordinatorBehavior extends CoordinatorLayout.Behavior<View> {
public CoordinatorBehavior(Context context, AttributeSet attrs) {
}
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
公共类CoordinatorBehavior扩展CoordinatorLayout.Behavior{
公共协调人行为(上下文、属性集属性){
}
公共布尔布局依赖项(协调布局父项、视图子项、视图依赖项){
返回Snackbar.Snackbar布局的依赖实例;
}
公共布尔onDependentViewChanged(协调布局父项、视图子项、视图依赖项){
float translationY=Math.min(0,dependency.getTranslationY()-dependency.getHeight());
child.setTranslationY(translationY);
返回true;
}
}更简单的方法
只需添加这一行:
app:layout_dodgeInsetEdges="bottom"
到您的布局
以下是一个例子:
<androidx.cardview.widget.CardView
android:id="@+id/fabImport"
android:layout_width="wrap_content"
android:layout_height="56dp"
app:cardCornerRadius="28dp"
android:foreground="?android:attr/selectableItemBackground"
android:backgroundTint="@color/colorPrimary"
android:layout_margin="16dp"
android:elevation="10dp"
app:layout_dodgeInsetEdges="bottom"
android:layout_gravity="bottom|start">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView...
更简单的方法
只需添加这一行:
app:layout_dodgeInsetEdges="bottom"
到您的布局
以下是一个例子:
<androidx.cardview.widget.CardView
android:id="@+id/fabImport"
android:layout_width="wrap_content"
android:layout_height="56dp"
app:cardCornerRadius="28dp"
android:foreground="?android:attr/selectableItemBackground"
android:backgroundTint="@color/colorPrimary"
android:layout_margin="16dp"
android:elevation="10dp"
app:layout_dodgeInsetEdges="bottom"
android:layout_gravity="bottom|start">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView...
我确实遇到过这个博客,但我已经在使用自定义文本视图MyCustomView扩展TextView@Rohit_Ramkumar您也可以将自定义行为类用于自定义textview.CoordinatorLayout.Behavior这不起作用。我需要给出CoordinatorLayout.Behavior,这给了我ClassCastException这是可行的,但也需要重写onDependentViewRemoved()并在子视图上调用setTranslationY(0)以恢复原始位置。如果Snackbar被刷掉,视图翻译将不会恢复。我确实遇到过这个博客,但我已经在使用自定义textview MyCustomView扩展TextView@Rohit_Ramkumar您也可以将自定义行为类用于自定义textview.CoordinatorLayout.Behavior这不起作用。我需要给出CoordinatorLayout.Behavior,这给了我ClassCastException这是可行的,但也需要重写onDependentViewRemoved()并在子视图上调用setTranslationY(0)以恢复原始位置。如果滑动浏览栏使其关闭,则不会恢复视图转换。