ConstraintLayout中的Z索引

时间:2018-09-03 20:44:35

标签: android android-layout z-index android-constraintlayout

我有一个简单的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做到这一点。

编辑: 我也已经尝试更改视图的顺序。这没用。

1 个答案:

答案 0 :(得分: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>