如何在 ConstraintLayout 中不使用边距 属性 的情况下相对于屏幕宽度在 2 个视图之间设置 space

How to set space between 2 views relative to screen width without using margin property in ConstraintLayout

我试过在两个视图(水平和垂直)之间设置 space 像这样

<?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"
    tools:context=".Fragments.SetProfileFragment">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="4dp"
        android:text="Test 1"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="4dp"
        android:text="Test 2"
        android:textSize="20sp"
        app:layout_constraintStart_toEndOf="@id/textView1"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

但我希望两个 TextViews 之间的 space 占屏幕的 1%。可能吗?

你可以试试这个代码

    val displayMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(displayMetrics)

    val width = displayMetrics.widthPixels
    val height = displayMetrics.heightPixels
    val widthPer = (1*100)/width  //1% percentage width
    val heightPer = (1*100)/height //1% percentage height
    textView1.setPadding(widthPer, heightPer, widthPer, heightPer)  //Adding padding
    textView2.setPadding(widthPer, heightPer, widthPer, heightPer) //Adding padding

but I want to spacing two TextView 1% percent of screen. is it possible?

  • 用于设置父级的开始和顶部约束: 使用 Horizontal & Vertical Guidelines 和根 ConstraintLayout 的百分比:

    1% 被认为是 0.01,因为 1% 等于全屏宽度 >> 所以,使用 app:layout_constraintGuide_percent="0.01"

    您可以将约束应用到 textview1,为了不重复自己,您可以将 textView2 约束到 textview1 的基线。

  • 用于设置 TextView 之间屏幕宽度的 1%: 使用 Space 约束到 [=16= 的末尾] 占屏幕宽度的1%:app:layout_constraintWidth_percent="0.01"。然后将 textView2 约束为 Space,而不是 textView.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.SetProfileFragment">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.01" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.01" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test 1"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="@id/guideline"
        app:layout_constraintTop_toTopOf="@id/guideline2" />

    <Space
        android:id="@+id/space"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintStart_toEndOf="@id/textView1"
        app:layout_constraintWidth_percent="0.01" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test 2"
        android:textSize="20sp"
        app:layout_constraintBaseline_toBaselineOf="@id/textView1"
        app:layout_constraintStart_toEndOf="@id/space"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>