【问题标题】:Make ScrollView smaller when snackbar appears出现小吃栏时使 ScrollView 变小
【发布时间】:2017-11-10 18:52:58
【问题描述】:

我刚开始在我的 android 应用程序中使用 Snackbar,我无法将 ScrollView 缩小到不覆盖 ScrollView 内部的任何内容。

这是我的活动的 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>

我在 StackOverflow 上尝试了很多解决方案,但都没有任何效果。

【问题讨论】:

  • 让你的scrollView变小是什么意思?
  • 好吧,通常它的高度设置为match_parent,但是当snackbar出现时我希望它比snackbar的高度小

标签: android android-design-library android-snackbar


【解决方案1】:

我的猜测是,对您来说最好的解决方案是实现 CoordinatorLayout 中指定的 page,如果您在链接中看到 GIF 格式,您可以看到浮动按钮在SnackBar 被触发,尝试用 ScrollView 元素代替教程中的 Floating Button,我想可以工作(无论如何,ScrollView 应该在其他属性之上..)

如果不起作用 可能您想扩展 ScrollView 并覆盖您需要的方法,如 google official 文档中所述,这应该始终有效,但您需要多做一些工作。这两种解决方案之一可能会成功。

【讨论】:

  • 遗憾的是,我无法完成这项工作,我的应用程序现在总是在显示快餐栏时崩溃。我已将我的ConstraintLayout 替换为android.support.design.widget.CoordinatorLayout,并将app:layout_behavior="sk.tuke.smart.makac.utils.FloatingActionButtonBehavior" 添加到我的ScrollView
  • 它甚至不会在日志中打印任何内容,但我找到了解决问题的方法,明天我会发布
  • 期待,给你一个upvote,因为我学到了一些东西:)
【解决方案2】:

所以,我找到了解决问题的方法,这就是解决方案。 我已在我的活动布局 xml 文件中将 ConstraintLayout 替换为 android.support.design.widget.CoordinatorLayout

<?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>

我创建了一个 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 的高度设置动画。

【讨论】:

  • 很高兴您按照我的建议使用 CoordinatorLayout。
  • 也许你应该接受我的回复,而不是自己做一个,毕竟我向你推荐了协调器布局,和/或至少你可以投票给我
猜你喜欢
  • 2020-10-18
  • 2017-07-09
  • 2016-07-05
  • 2019-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多