【问题标题】:Using ConstraintLayout, can I get the vertical constraint to match the dynamic width of a chain?使用 ConstraintLayout,我可以获得垂直约束以匹配链的动态宽度吗?
【发布时间】:2017-12-03 22:02:09
【问题描述】:

我的布局中有四个按钮的网格。我将它们水平链接起来,以便左右按钮之间的空间均匀加权。

screenshot of my app's buttons

我是否可以让两排按钮在它们之间具有相同的垂直间距,这样看起来才不会不均匀?我宁愿不只是像我为实现水平间距所做的那样将它们垂直居中,这要感谢这篇文章的@AdamK:Evenly spacing views using ConstraintLayout

activity_main.xml:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jason.notifier.MainActivity"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="388dp"
        android:layout_height="164dp"
        app:srcCompat="@drawable/banner"
        android:layout_marginStart="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent" />

    <Button
        android:id="@+id/testbutton"
        android:layout_width="171dp"
        android:layout_height="136dp"
        android:onClick="sampleNotification"
        android:text="Test"
        app:layout_constraintRight_toLeftOf="@+id/settingsbutton"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/historybutton" />

    <Button
        android:id="@+id/settingsbutton"
        android:layout_width="171dp"
        android:layout_height="136dp"
        android:text="Settings"
        android:onClick="settingsbutton"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/testbutton"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/pingbutton" />

    <Button
        android:id="@+id/pingbutton"
        android:layout_width="171dp"
        android:layout_height="136dp"
        android:onClick="pingbutton"
        android:text="ping"
        android:layout_marginTop="-31dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/historybutton" />

    <Button
        android:id="@+id/historybutton"
        android:layout_width="171dp"
        android:layout_height="136dp"
        android:onClick="historybutton"
        android:text="History"
        android:layout_marginTop="-31dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintRight_toLeftOf="@+id/pingbutton"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="358dp"
        android:layout_height="42dp"
        android:text="TextView"
        android:gravity="center"
        tools:layout_editor_absoluteX="2dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 分享你的约束布局 XML

标签: android android-constraintlayout android-studio-2.3


【解决方案1】:

View 小部件放置在布局的中心,尺寸比为 1:1,左侧受historybutton 约束,右侧受pingbutton 约束。这将为您提供一个正方形,其中每一边都是水平间隙的长度。

<View
    android:id="@+id/spacerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="0dp"
    app:layout_constraintDimensionRatio="H,1:1"
    app:layout_constraintEnd_toStartOf="@+id/pingbutton"
    app:layout_constraintStart_toEndOf="@+id/historybutton"
    app:layout_constraintTop_toBottomOf="@+id/historybutton" />

现在将底部的两个按钮约束到正方形的底部,如下所示:

<Button
    android:id="@+id/testbutton"
    android:layout_width="171dp"
    android:layout_height="136dp"
    android:text="Test"
    app:layout_constraintTop_toBottomOf="@+id/spacerView"
    app:layout_constraintEnd_toEndOf="@+id/historybutton" />

<Button
    android:id="@+id/settingsbutton"
    android:layout_width="171dp"
    android:layout_height="136dp"
    android:text="Settings"
    app:layout_constraintTop_toBottomOf="@+id/spacerView"
    app:layout_constraintStart_toStartOf="@+id/pingbutton" />

应该这样做。我让方形视图可见,以便我们可以看到它,但你应该让它不可见。这是效果图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2020-02-27
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多