【问题标题】:How to push up an existing view when snackbar is displayed?显示快餐栏时如何向上推现有视图?
【发布时间】:2016-07-31 09:33:41
【问题描述】:

这是我在snackbar 可见时向上推“NEXT”按钮的尝试:

如您所见,它没有按计划工作。看起来好像textview 被推到RelativeLayout 后面,然后在snackbar 消失时重新出现。相反,我想要的是 textview 出现向上推(所以它在 snackbar 上方),然后在 snackbar 消失时返回。我还创建了一个小型 github 存储库来演示这一点:

https://github.com/Winghin2517/TestFabMotion

这是CoordinatorLayout.Behavior,我用于“NEXT”隐藏自身。当snackbar 出现时,该行为取自FAB 行为:

public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<TextView> {

    public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, TextView child, View dependency) {

        return dependency instanceof Snackbar.SnackbarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, TextView child, View dependency) {

        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }
}

我已经尝试过使用这个 xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Click Here!"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:background="@color/colorAccent"
            android:id="@+id/click_here"
            android:textSize="22sp"
            android:layout_marginBottom="12dp"
            android:gravity="center_horizontal"
            android:paddingTop="24dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/click_here"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/next"
            android:text="Next"
            android:layout_alignParentBottom="true"
            android:background="@color/colorAccent"
            android:clickable="true"
            android:gravity="center_horizontal"
            android:padding="16dp"
            android:textAllCaps="true"
            android:textStyle="bold"
            app:layout_behavior="fabmotiontest.com.fabtest.FloatingActionButtonBehavior" />

    </RelativeLayout>

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

</LinearLayout>

我也尝试过使用这个xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Click Here!"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:background="@color/colorAccent"
            android:id="@+id/click_here"
            android:textSize="22sp"
            android:layout_marginBottom="12dp"
            android:gravity="center_horizontal"
            android:paddingTop="24dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/click_here"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/ic_launcher"/>

    </RelativeLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorlayout"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/next"
            android:text="Next"
            android:background="@color/colorAccent"
            android:clickable="true"
            android:gravity="center_horizontal"
            android:padding="16dp"
            android:textAllCaps="true"
            android:textStyle="bold"
            app:layout_behavior="fabmotiontest.com.fabtest.FloatingActionButtonBehavior" />

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

</LinearLayout>

它似乎不起作用。有谁知道我怎样才能让它工作?

【问题讨论】:

    标签: android behavior android-coordinatorlayout android-snackbar


    【解决方案1】:

    在测试了几种解决方案后,我发现了一种可行的 xml 布局。

    我之前尝试过的和下面的主要区别在于coordinatorlayout 的高度是“match_parent”,这意味着它覆盖了它下面的视图,但是因为coordinatorlayout 是透明的,所以你看不到它和当snackbar 从底部向上推时,它现在将有足够的空间将textview 推到snackbar 上方:

    <?xml version="1.0" encoding="utf-8"?>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_weight="1">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Click Here!"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:background="@color/colorAccent"
                android:id="@+id/click_here"
                android:textSize="22sp"
                android:layout_marginBottom="12dp"
                android:gravity="center_horizontal"
                android:paddingTop="24dp"
                android:paddingLeft="24dp"
                android:paddingRight="24dp" />
    
            <ImageButton
                android:id="@+id/btnComments"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_below="@+id/click_here"
                android:layout_centerHorizontal="true"
                android:background="@drawable/shadow_bg"
                android:src="@mipmap/ic_launcher" />
    
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinatorlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal|bottom">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/next"
                android:text="Next"
                android:background="@color/colorAccent"
                android:clickable="true"
                android:gravity="center_horizontal"
                android:padding="16dp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:layout_gravity="center_horizontal|bottom"
                app:layout_behavior="fabmotiontest.com.fabtest.FloatingActionButtonBehavior" />
    
        </android.support.design.widget.CoordinatorLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 嗨,西蒙,在我的情况下,我必须将 Wrap content 设置为 cardView 以将其显示在底部,但是当小吃店显示 cardview 上升但由于 wrap 内容而被剪切但将其设置为匹配父母把它放在所有视图的顶部。请建议。! RL -> Cordinator -> CardView(带有 fab 属性)-> Relativelayout(带有两个图像)
    【解决方案2】:

    那又怎样:

        Button button = (Button)findViewById(R.id.next);
    button.setLayoutParams(new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams,
        myNewX, myNewY));
    

    我不知道用哪个x和y坐标,你得试试……

    【讨论】:

    • 这不会为按钮设置动画。这只会调整它的大小。
    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 2020-12-12
    • 2022-09-27
    • 2017-09-14
    • 2020-11-04
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多