对齐ConstraintLayout上的按钮

时间:2018-05-15 11:29:53

标签: android android-layout

我希望在toolbar上有5个按钮彼此相等的距离。

现在我只是想把它们放在正确的顺序上,将它们对齐到彼此的末尾。由于某些原因,这不起作用,其中三个按钮只是在布局开始时呈现,我无法弄清楚原因。

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#131314"
android:elevation="4dp">
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativeLayout">

    <Button
        android:id="@+id/btn_camera"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginStart="50dp"
        android:background="@drawable/camera"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btn_ruler"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:background="@drawable/ruler"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/btn_camera"/>

    <Button
        android:id="@+id/btn_3"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:background="@drawable/crop"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/btn_ruler" />

    <Button
        android:id="@+id/btn_price"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:background="@drawable/euro"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/btn_3" />

    <Button
        android:id="@+id/btn_5"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="60dp"
        android:background="@drawable/camera"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

</android.support.v7.widget.Toolbar>

结果如下:

enter image description here

非常感谢任何帮助,谢谢!

3 个答案:

答案 0 :(得分:3)

尝试更改约束元素以使用@+id/...

例如更改:

app:layout_constraintStart_toEndOf="@id/btn_camera"/>

要:

app:layout_constraintStart_toEndOf="@+id/btn_camera"/>

答案 1 :(得分:1)

试试这个

@require

<强>输出

enter image description here

答案 2 :(得分:0)

select t.*
from t
order by right(date, 4) desc, left(date, 2) asc
limit 1;