ConstraintLayout中的Z-index

问题描述 投票:0回答:1

我有一个简单的ConstraintLayout

<android.support.constraint.ConstraintLayout
    android:id="@+id/basketLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <TextView
            android:id="@+id/basketItemnumber"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/basket_item_number"
            android:text="1"
            android:textColor="@color/white"
            android:gravity="center"
            app:layout_constraintBottom_toBottomOf="@id/marginSpacer"
            app:layout_constraintEnd_toEndOf="@id/shoppingBasketButton"/>

        <android.support.v4.widget.Space
            android:id="@+id/marginSpacer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="15dp"
            app:layout_constraintTop_toTopOf="@+id/shoppingBasketButton"
            app:layout_constraintLeft_toLeftOf="@id/shoppingBasketButton"
            app:layout_constraintRight_toRightOf="@id/shoppingBasketButton" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/shoppingBasketButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginEnd="15dp"
            android:layout_marginRight="15dp"
            android:clickable="true"
            android:focusable="auto"
            android:focusableInTouchMode="false"
            app:backgroundTint="@color/colorPrimary"
            app:srcCompat="@drawable/ic_shopping_cart"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

现在shoppingBasketButtonbasketItemnumber重叠,但我希望反过来。

但我无法弄清楚如何更改项目的z-index。

我尝试了setTranslationZsetZ,但没有成功。

bringToFront()也不适合我。

编辑:我设法使用它:

ViewCompat.setZ(basketItemnumber, 999);
basketItemnumber.invalidate();

但我无法为API <21做到这一点。

编辑:我也尝试过改变观点的顺序。这没用。

android android-layout z-index android-constraintlayout
1个回答
0
投票

如果我理解正确,你可以把你的basketItemnumber放在shoppingBasketButton之后,它可以解决你的问题。

   <android.support.constraint.ConstraintLayout
    android:id="@+id/basketLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.Space
        android:id="@+id/marginSpacer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="15dp"
        app:layout_constraintTop_toTopOf="@+id/shoppingBasketButton"
        app:layout_constraintLeft_toLeftOf="@id/shoppingBasketButton"
        app:layout_constraintRight_toRightOf="@id/shoppingBasketButton" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/shoppingBasketButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:clickable="true"
        android:focusable="auto"
        android:focusableInTouchMode="false"
        app:backgroundTint="@color/colorPrimary"
        app:srcCompat="@drawable/ic_shopping_cart"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/basketItemnumber"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="@drawable/basket_item_number"
        android:text="1"
        android:textColor="@color/colorBlack"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="@id/marginSpacer"
        app:layout_constraintEnd_toEndOf="@id/shoppingBasketButton"/>

</android.support.constraint.ConstraintLayout>
© www.soinside.com 2019 - 2024. All rights reserved.