[英]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.