简体   繁体   English

如何在约束布局中设置边距百分比?

[英]How set margin percentage in a constraint layout?

I want to create a screen with a margin of 1: 2: 2.我想创建一个边距为 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也许app:layout_constraintVertical_weight可以帮助你

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在约束布局中设置(以编程方式)Gone Margin? - How to set (programatically) Gone Margin in Constraint Layout? 如何为约束布局组提供垂直边距? - How to give vertical margin to constraint layout group? 如何在约束布局上实现重叠/负边距? - How to achieve overlap/negative margin on Constraint Layout? 如何在约束布局中给出百分比权重 android - How to give percentage weight in constraint layout android 约束布局意外边距 - constraint layout unexpected margin 如何以编程方式更改约束布局中的约束边距? - how to change constraint margin in my constraint layout programatically? 如何将recyclerview中项目的约束布局宽度设置为父宽度的百分比,而不是固定的dp值? - How to set a constraint layout width of an item in a recyclerview to a percentage of the parents width, not a fixed dp value? 约束布局,百分比无法按预期工作 - Constraint Layout with percentage not working as expected 是否可以在距离约束布局中设置障碍,或者在没有约束的情况下向边视图添加边距? - Any way to set barrier at a distance constraint layout or adding margin to a view the side with no constraint? 如何在相对布局中设置文本视图的页边距 - How to set Margin of a textview in a relative layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM