自定义视图不遵守约束布局中的约束
Custom View not adhering to Constraints in Constraint Layout
我的项目中有一个自定义的 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>
结果
我终于明白了。似乎约束布局在使用包含线性布局的布局资源的自定义视图方面存在一些问题。为了实现我的 objective,我将父布局从 Constraint 布局更改为 Frame 布局,并使用重力来保持位置,现在所有运行都完美无缺。
我的项目中有一个自定义的 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>
结果
我终于明白了。似乎约束布局在使用包含线性布局的布局资源的自定义视图方面存在一些问题。为了实现我的 objective,我将父布局从 Constraint 布局更改为 Frame 布局,并使用重力来保持位置,现在所有运行都完美无缺。