将textField结束约束设置为parentView中心X使用约束布局

问题描述 投票:0回答:1

我有2个文本字段。我想将textField 1 end和textField 2开始设置为父视图中心X。我的方法是使用具有布局边距的等宽文本字段。如何使用androidconstraintlayout进行此操作。

What I want

android android-constraintlayout
1个回答
0
投票

您可以使用Guideline]实现此目的

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt"
    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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <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=".5" />


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Ref :: https://developer.android.com/reference/androidx/constraintlayout/widget/Guideline

准则可以是水平或垂直:

  • 垂直准则的宽度为零,其ConstraintLayout父级的高度为高
  • 水平准线的高度为零,其ConstraintLayout父级的宽度为一个
  • 可以通过三种不同方式定位准则:

  • 指定从布局的左侧或顶部的固定距离(layout_constraintGuide_begin
  • 指定从布局的右侧或底部的固定距离(layout_constraintGuide_end
  • 指定版式的​​宽度或高度的百分比(layout_constraintGuide_percent
© www.soinside.com 2019 - 2024. All rights reserved.