当 SnackBar 出现在 CoordinatorLayout 中时上移视图

Posted

技术标签:

【中文标题】当 SnackBar 出现在 CoordinatorLayout 中时上移视图【英文标题】:Move views up when SnackBar appears in CoordinatorLayout 【发布时间】:2017-07-31 06:10:25 【问题描述】:

我在CoordinatorLayout 的底部有一个TextView

但是当我显示 SnackBar 时,它会覆盖 TextView

我知道我必须为TextView 自定义Behavior 并覆盖layoutDependsOnonDependentViewChanged,但它并不能很好地解决。

如果你知道,能给我一些建议吗?谢谢。

【问题讨论】:

如果你在这里添加你的xml并解释一下,你的意思会更清楚,你的要求是什么? 我只想知道如何在 SnackBar 显示时保留 SnackBar 上方的 textview 并在 SnackBar 消失时重置其位置。如果我什么都不做,则 SnackBar 将覆盖 myTextView 。 【参考方案1】:

如果 TextView 是 CoordinatorLayout 的直接子级,只需添加

app:layout_dodgeInsetEdges="bottom"

在 TextView 属性中。

魔法!

【讨论】:

这样的工作。但它与具有行为的解决方案具有相同的情况。一些视图如何在显示小吃店开始时跳转。【参考方案2】:

您需要向LinearLayout 添加一个行为并将其嵌入CoordinatorLayout

这是你的做法。

MoveUpwardBehavior.class

import android.os.Build;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.view.View;


public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> 
    private static final boolean SNACKBAR_BEHAVIOR_ENABLED;

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) 
        return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout;
    

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) 
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    

    static 
        SNACKBAR_BEHAVIOR_ENABLED = Build.VERSION.SDK_INT >= 11;
    

CustomLinearLayout.class

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.widget.LinearLayout;

@CoordinatorLayout.DefaultBehavior(MoveUpwardBehavior.class)
public class CustomLinearLayout extends LinearLayout 
    public CustomLinearLayout(Context context) 
        super(context);
    

    public CustomLinearLayout(Context context, AttributeSet attrs) 
        super(context, attrs);
    

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) 
        super(context, attrs, defStyleAttr);
    

示例 xml->activity_home

这里 user.example.charu.its2017huree 是我的包名替换为你的!

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_
        android:fitsSystemWindows="true"
        android:layout_>
        <user.example.charu.its2017huree.CustomLinearLayout
            android:background="#098"
            android:gravity="bottom"
            android:id="@+id/linearLayout"
            android:layout_
            android:layout_
            android:orientation="vertical">
            <TextView
                android:layout_
                android:layout_
                android:text="Hello world" />
        </user.example.charu.its2017huree.CustomLinearLayout>

终于在我的Activity中叫HomeActivity

public class HomeActivity extends Activity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        CustomLinearLayout customLinearLayout = (CustomLinearLayout) findViewById(R.id.linearLayout);
        Snackbar.make(customLinearLayout, "Text to display", Snackbar.LENGTH_LONG).show();

    


来源来自这个example。

【讨论】:

非常感谢。onDependentViewChanged 块是我想要的。非常感谢。 @Charu DefaultBehaviour 已弃用?

以上是关于当 SnackBar 出现在 CoordinatorLayout 中时上移视图的主要内容,如果未能解决你的问题,请参考以下文章

从 AlertDialog 中调用 SnackBar

使 Snackbar 向上推视图

如何像浮动按钮一样在 Snackbar 上方移动视图

Android开发——Snackbar使用详解

当窗口太小时,避免 SnackBar 全宽

当用户在其他地方交互时如何关闭 Snackbar?