简体   繁体   中英

How set margin percentage in a constraint layout?

I want to create a screen with a margin of 1: 2: 2.
Is this possible if I make this a constraint layout?
Please let me know if there is a better way.

Below is the image and code. 图片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        tools:context=".MainActivity">

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Title" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:text="Button" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2" />

</LinearLayout>

Maybe app:layout_constraintVertical_weight can help you

<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">

    <View
        android:id="@+id/space1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="1" />

    <Button
        android:id="@+id/button"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Title"
        app:layout_constraintBottom_toTopOf="@id/space2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/space1" />

    <View
        android:id="@+id/space2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/button2"
        app:layout_constraintTop_toBottomOf="@id/button"
        app:layout_constraintVertical_weight="2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@id/space3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/space2" />

    <View
        android:id="@+id/space3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button2"
        app:layout_constraintVertical_weight="2" />

</androidx.constraintlayout.widget.ConstraintLayout>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM