如何在显示小吃栏时推高现有视图?

问题描述 投票:6回答:2

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

如您所见,它无法按计划运行。似乎textview被推上并且在RelativeLayout后面,然后当snackbar消失时再次出现。相反,我想要的是textview看起来被推高(因此它在snackbar之上)然后当snackbar消失时再回来。我还创建了一个小的github repo来证明这一点:

https://github.com/Winghin2517/TestFabMotion

这是我用于“NEXT”隐藏自己的CoordinatorLayout.Behavior。当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
2个回答
2
投票

在测试了几个解决方案之后,我遇到了一个可行的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>

0
投票

那个怎么样:

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

我不知道要使用哪个x和y坐标,你将不得不尝试...

© www.soinside.com 2019 - 2024. All rights reserved.