我的项目中有一个自定义的FrameLayout,它充当按钮,因为我不想继续重复相同的代码。视图效果很好,但是当我需要将它们水平约束到彼此时,中心视图不会遵循约束,相反,它与父开始相对应,使另一个更加黯然失色。我应该如何处理我的代码以确保约束按预期工作?代码如下
dash_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.revosleap.wazalendo.utils.ui.DashButton
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/dashButtonLoan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:dash_text="Pay"
app:dash_icon="@drawable/ic_pay"
android:id="@+id/dashButtonPay"
/>
<com.revosleap.wazalendo.utils.ui.DashButton
app:layout_constraintStart_toEndOf="@id/dashButtonPay"
app:layout_constraintEnd_toStartOf="@id/dashButtonAccount"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:dash_text="Loan"
app:dash_icon="@drawable/ic_loan"
android:id="@+id/dashButtonLoan"
/>
<com.revosleap.wazalendo.utils.ui.DashButton
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/dashButtonLoan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:dash_text="Account"
app:dash_icon="@drawable/ic_user"
android:id="@+id/dashButtonAccount"
/>
</android.support.constraint.ConstraintLayout>
结果
我终于弄明白了。似乎Constraint Layout对使用包含线性布局的布局资源的自定义视图存在一些问题。为了实现我的目标,我将父布局从Constraint布局更改为Frame布局,并使用gravity来保持位置,并且所有运行现在都完美无缺。