Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中Snackbar.make()方法的第一个参数_Android_Material Design_Android Coordinatorlayout - Fatal编程技术网

Android中Snackbar.make()方法的第一个参数

Android中Snackbar.make()方法的第一个参数,android,material-design,android-coordinatorlayout,Android,Material Design,Android Coordinatorlayout,我不熟悉材料设计。当我在搜索如何显示snackbar时,我发现有两种不同的方法: new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(mCoordinatorLayout,... }} new View.OnClickListener() { @Override public void on

我不熟悉材料设计。当我在搜索如何显示snackbar时,我发现有两种不同的方法:

new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(mCoordinatorLayout,...
}}
new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view,...
}}
这两种方法都很好。我不明白为什么?我认为视图指的是按钮对象,mCoordinatorLayout指的是CoordinatorLayout对象。它们是不同的对象,但两个对象中的任何一个都可以作为Snackbar.make()方法的第一个参数。为什么?

完整的源代码如下所示:

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout ...    >

<Button
    android:id="@+id/showSnackbarButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/show_snackbar"/>

</android.support.design.widget.CoordinatorLayout>

如果您直接传递CoordinatorLayout,它将避免在层次结构中“向上”查找CoordinatorLayout。因此,您可以避免进行一些计算

如果您使用一个视图,它将尝试在window decor的内容视图上找到一个协调布局或回退

这是Snackbar中用于查找“合适的父项”的方法:


其实很简单

如果您查看
Snackbar
的源代码,您将看到在创建
Snackbar
时,有一个名为
findSuitableParent
的私有方法调用,该方法采用
视图(您给出的视图)并继续在视图层次结构中循环,直到找到一个
CoordinatorLayout
contentView
,该视图保存屏幕中的所有内容

见:

从何处调用

我在为一个项目分叉代码时发现了这一点,在这个项目中,我必须将
Snackbar
从屏幕底部移动到顶部;-)

希望这有帮助

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);

    mShowSnackbarButton = (Button) findViewById(R.id.showSnackbarButton);
    mShowSnackbarButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(mCoordinatorLayout,
                    "This is a simple Snackbar", Snackbar.LENGTH_LONG)
                    .setAction("CLOSE", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // Custom action
                            Toast.makeText(MainActivity.this,"this is a toast message", Toast.LENGTH_SHORT).show();
                        }
                    }).show();
        }
    });
}
private static ViewGroup findSuitableParent(View view) {
    ViewGroup fallback = null;
    do {
        if (view instanceof CoordinatorLayout) {
            // We've found a CoordinatorLayout, use it
            return (ViewGroup) view;
        } else if (view instanceof FrameLayout) {
            if (view.getId() == android.R.id.content) {
                // If we've hit the decor content view, then we didn't find a CoL in the
                // hierarchy, so use it.
                return (ViewGroup) view;
            } else {
                // It's not the content view but we'll use it as our fallback
                fallback = (ViewGroup) view;
            }
        }

        if (view != null) {
            // Else, we will loop and crawl up the view hierarchy and try to find a parent
            final ViewParent parent = view.getParent();
            view = parent instanceof View ? (View) parent : null;
        }
    } while (view != null);

    // If we reach here then we didn't find a CoL or a suitable content view so we'll fallback
    return fallback;
}