出现 snackbar 时使 ScrollView 变小
Make ScrollView smaller when snackbar appears
我刚开始在我的 android 应用程序中使用 Snackbar,但我无法将 ScrollView 缩小到不覆盖 ScrollView 内部的任何内容。
这是我的 activity 的 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
tools:context="com.example.MainActivity">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:orientation="vertical">
[LOT OF ELEMENTS IN HERE]
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
我在 Whosebug 上尝试了很多解决方案,但还没有一个对我有用。
我的猜测是,如果您在 link 中看到 GIF 格式,那么对您来说最好的解决方案是实现此 page 中指定的 CoordinatorLayout
,您可以看到触发 SnackBar 时 Floating Botton 如何升起的动画,尝试用 ScrollView 元素代替教程中的 Floating Button,我想可以工作(无论如何 ScrollView 应该在其他属性之上..)
如果不起作用
可能您想扩展 ScrollView 并覆盖您需要的方法,如 google official 文档中所述,这应该始终有效,但您需要稍微 more.One 两种解决方案可能会成功。
所以,我找到了解决问题的办法,这就是解决办法。
我在 activity 的布局 xml 文件中用 android.support.design.widget.CoordinatorLayout
替换了 ConstraintLayout
。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
tools:context="com.example.MainActivity">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.example.DecreaseHeightBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:orientation="vertical">
[ELEMENTS HERE]
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
并且我创建了一个 class DecreaseHeightBehavior,它看起来像这样:
package com.example;
import android.content.Context;
import android.support.annotation.Keep;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
@Keep
public class DecreaseHeightBehavior extends CoordinatorLayout.Behavior<View>{
public DecreaseHeightBehavior(){
super();
}
public DecreaseHeightBehavior(Context context, AttributeSet attrs){
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency){
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency){
ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
layoutParams.height = parent.getHeight() - dependency.getHeight();
child.setLayoutParams(layoutParams);
return true;
}
@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency){
ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
layoutParams.height = MATCH_PARENT;
child.setLayoutParams(layoutParams);
}
}
这对我有用,但此解决方案不会为 ScrollView 的高度设置动画。
我刚开始在我的 android 应用程序中使用 Snackbar,但我无法将 ScrollView 缩小到不覆盖 ScrollView 内部的任何内容。
这是我的 activity 的 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
tools:context="com.example.MainActivity">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:orientation="vertical">
[LOT OF ELEMENTS IN HERE]
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
我在 Whosebug 上尝试了很多解决方案,但还没有一个对我有用。
我的猜测是,如果您在 link 中看到 GIF 格式,那么对您来说最好的解决方案是实现此 page 中指定的 CoordinatorLayout
,您可以看到触发 SnackBar 时 Floating Botton 如何升起的动画,尝试用 ScrollView 元素代替教程中的 Floating Button,我想可以工作(无论如何 ScrollView 应该在其他属性之上..)
如果不起作用 可能您想扩展 ScrollView 并覆盖您需要的方法,如 google official 文档中所述,这应该始终有效,但您需要稍微 more.One 两种解决方案可能会成功。
所以,我找到了解决问题的办法,这就是解决办法。
我在 activity 的布局 xml 文件中用 android.support.design.widget.CoordinatorLayout
替换了 ConstraintLayout
。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
tools:context="com.example.MainActivity">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.example.DecreaseHeightBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:orientation="vertical">
[ELEMENTS HERE]
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
并且我创建了一个 class DecreaseHeightBehavior,它看起来像这样:
package com.example;
import android.content.Context;
import android.support.annotation.Keep;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
@Keep
public class DecreaseHeightBehavior extends CoordinatorLayout.Behavior<View>{
public DecreaseHeightBehavior(){
super();
}
public DecreaseHeightBehavior(Context context, AttributeSet attrs){
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency){
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency){
ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
layoutParams.height = parent.getHeight() - dependency.getHeight();
child.setLayoutParams(layoutParams);
return true;
}
@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency){
ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
layoutParams.height = MATCH_PARENT;
child.setLayoutParams(layoutParams);
}
}
这对我有用,但此解决方案不会为 ScrollView 的高度设置动画。