代码之家  ›  专栏  ›  技术社区  ›  J. Doe

空间不足时,TextView居中

  •  0
  • J. Doe  · 技术社区  · 3 年前

    我有一个非常基本的 ConstraintLayout 用一个 TextView Button 具有以下要求:

    1. 这个 约束布局 宽度应为的最大宽度的大小 文本框 按钮 (已生效)
    2. 文本框 就宽度而言,不是最大的视图,它应该与末端对齐(当前对齐在中间时 按钮 较大)
    3. 的高度 约束布局 应该是 文本框 高度+ 按钮 高度,目前它有额外的填充

    现在看起来是这样的:

    enter image description here

    这是我的XML:

    <?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">
    
    
        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:id="@+id/constraintLayout">
    
            <Button
                    android:text="ssdsa ddsdsda"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/button"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginBottom="1dp"
                    app:layout_constraintBottom_toTopOf="@+id/textView" />
    
            <TextView
                    android:text="texttttttttttttt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   Dinesh    3 年前

    在您的 xml 文件来自 TextView 只需移除 app:layout_constraintStart_toStartOf="parent" 它将根据您的要求工作。

    要去除底部的额外衬垫,也要去除 app:layout_constraintTop_toTopOf="parent" 这行来自 文本框

        <?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">
    
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/constraintLayout">
    
            <Button
                android:text="ssdsa ddsdsda"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginBottom="1dp"
                app:layout_constraintBottom_toTopOf="@+id/textView" />
    
            <TextView
                android:text="texttttttttttttt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>