Android:在元素和边框之间创建空格(等间距)

时间:2019-11-18 13:28:14

标签: android android-layout android-constraintlayout

我有两个imageView小部件,我想放置它们,以便在左边框和左图像,左图片和右图像以及右图片和右边框之间有相等的空间。 (我在问题空格的标题中写过,因为它类似于CSS中使用的间距...)

是否可以不进行手动调整(例如计算边距)而实现?无需编写xml代码,是否可以仅在设计模式下实现?

屏幕截图: enter image description here

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="@drawable/newbackground"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnRoll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#3498db"
        android:text="@string/btnRoll"
        android:textColor="#ffffff"
        tools:layout_editor_absoluteX="167dp"
        tools:layout_editor_absoluteY="621dp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@+id/imageView3"
        app:srcCompat="@drawable/dice1"
        tools:layout_editor_absoluteX="59dp" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/dice2"
        tools:layout_editor_absoluteX="267dp"
        tools:layout_editor_absoluteY="406dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

2 个答案:

答案 0 :(得分:2)

尝试一下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
        android:background="@drawable/newbackground"
        tools:context=".MainActivity">

    <Button
            android:id="@+id/btnRoll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#3498db"
            android:text="btnRoll"
            android:textColor="#ffffff"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@id/btnRoll"
            app:layout_constraintEnd_toStartOf="@+id/imageView3"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:srcCompat="@drawable/blue_heart" />

    <ImageView
            android:id="@+id/imageView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            app:layout_constraintBottom_toTopOf="@id/btnRoll"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/imageView2"
            app:srcCompat="@drawable/blue_heart" />
</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

答案 1 :(得分:1)

这是一个带有LinearLayout的命题

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
  <View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />
  <ImageView ... />
  <View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />
  <ImageView ... />
  <View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />
  <ImageView ...
</LinearLayout>

使用layout_weight属性,“ spacer”视图将平均占用最大可用空间