对自定义视图使用约束布局

时间:2018-07-13 12:48:03

标签: android xml android-layout android-constraintlayout

我正在使用布局编辑器中的拖放功能来尝试获得4个以风筝形状对齐的图像按钮。这是我正在尝试实现的布局,当前使用ImageButton,但稍后将更改为圆形图像视图以适合该布局。 Screen grab of the layout goal

任何人都可以帮助我实现这一目标。请在下面找到我的XML:

<android.support.constraint.ConstraintLayout
    android:id="@+id/ongoing_call_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/activity_padding"
    android:layout_below="@id/contact_number_tv"
    android:layout_above="@id/calculator_layout">

    <ImageButton
        android:id="@+id/home_imgBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:src="@android:drawable/btn_star"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/not_billable_imgBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="104dp"
        android:src="@android:drawable/btn_star"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/billable_imgBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/btn_star"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/not_billable_imgBtn"
        tools:layout_editor_absoluteY="105dp" />


    <ImageButton
        android:id="@+id/decide_later_imgBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="120dp"
        android:src="@android:drawable/btn_star"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />


</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

尝试这个伙伴,然后进行自己的调整。

<android.support.constraint.ConstraintLayout 
    android:id="@+id/ongoing_call_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/activity_padding"
    android:layout_below="@id/contact_number_tv"
    android:layout_above="@id/calculator_layout">

    <ImageButton
        android:id="@+id/home_imgBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:src="@android:drawable/btn_star"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.32"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/not_billable_imgBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:src="@android:drawable/btn_star"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/billable_imgBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:src="@android:drawable/btn_star"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.66"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/decide_later_imgBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="60dp"
        android:layout_marginBottom="24dp"
        android:src="@android:drawable/btn_star"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/not_billable_imgBtn" />

 </android.support.constraint.ConstraintLayout>

See the example image

但是也许您真正的问题是constrainLayout不是您的根元素,所以应该设置一个固定的高度而不是android:layout_height="wrap_content"

相关问题