android - 自定义 View 不遵守约束布局中的约束

标签 android android-custom-view android-constraintlayout

我的项目中有一个自定义的 FrameLayout 作为按钮,因为我不想重复相同的代码。 该 View 工作完美,但是当我需要将它们水平排列并相互约束时,中心 View 不遵循约束,而是与父 View 对齐,开始遮蔽另一个 View 。 我应该如何处理我的代码以确保约束按预期工作? 代码如下

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>

结果

The result I get

最佳答案

我终于明白了。似乎约束布局在使用包含线性布局的布局资源的自定义 View 方面存在一些问题。为了实现我的目标,我将父布局从 Constraint 布局更改为 Frame 布局,并使用重力来保持位置,现在所有运行都完美无缺。

关于android - 自定义 View 不遵守约束布局中的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54765713/

相关文章:

android - 仅限wifi通讯

java - 使用进度对话框和处理程序

java - 获取自定义适配器android的textview

android - Android 中的椭圆 View

android - ConstraintLayout 中的 TextView 剪辑文本和 layout_margin 无法正确显示

android - 添加值后增加共享首选项键

android - 从 fbsdk React Native 获取数据

android - 在谷歌地图中绘制 ARC 折线

android - 无法将 FrameLayout 放置在 ConstraintLayout 内

android - 子约束布局的约束布局不水平扩展