约束布局中的 Z 索引



我有一个简单的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 索引。

我尝试了setTranslationZsetZ但没有成功。

bringToFront()也对我不起作用。

编辑:我设法让它与:

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

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

编辑:我也已经尝试改变我的观点顺序。这行不通。

如果我理解正确,您可以将您的篮子项目编号放在购物篮按钮之后,它可以解决您的问题。

<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>

最新更新