当SnackBar出现在CoordinatorLayout中时,将视图向上移动 [英] Move views up when SnackBar appears in CoordinatorLayout

查看:217
本文介绍了当SnackBar出现在CoordinatorLayout中时,将视图向上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CoordinatorLayout的底部有一个TextView.

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

But when I show a SnackBar , it will cover the TextView.

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

I know I have to customize a Behavior for the TextView and override layoutDependsOn and onDependentViewChanged,but it doesn't fix very well.

如果您知道,可以给我一些建议吗?谢谢.

Could you give me some advice if you know? Thanks.

推荐答案

您需要将行为添加到LinearLayout并将其嵌入到CoordinatorLayout中.

You need to add a behavior to your LinearLayout and embed it in a 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 是我的软件包名称,请用您的软件包名称替换!

Here user.example.charu.its2017huree is my package name replace it with yours!

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

最后在我的活动中,该活动称为 HomeActivity

Finally in my Activity called 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();

    }

}

来源来自以下示例.

这篇关于当SnackBar出现在CoordinatorLayout中时,将视图向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆