【问题标题】:Remove space between two views in chained view in constraint layout删除约束布局中链式视图中两个视图之间的空间
【发布时间】:2020-01-31 17:46:52
【问题描述】:

我在约束布局中使用链式视图。现在我希望删除两个文本视图之间的中间空间。因为底部的按钮距离较远。我试图将它们从设计布局中移出,但它也没有发生。怎么做。以下是我的xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_editor_absoluteY="81dp">


    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="23dp"
        android:layout_marginStart="32dp"
        android:gravity="center"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="@+id/imageView3"
        app:layout_constraintStart_toEndOf="@+id/imageView3" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="26dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/textViewHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!--<LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="1dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="60dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/linearLayout2">-->


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="25dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="70dp"
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="32dp"
        android:layout_marginTop="26dp"
        android:gravity="center"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/imageView2"
        app:layout_constraintTop_toTopOf="@+id/imageView2" />

    <!-- </LinearLayout>-->

    <!--  <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="1dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="60dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="@id/linearLayout"
       >-->


    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="17dp"
        android:layout_gravity="center"
        android:layout_marginBottom="58dp"
        android:layout_marginEnd="213dp"
        android:layout_marginTop="58dp"
        android:gravity="center"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/imageView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/textViewHeader" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="67dp"
        android:layout_marginStart="90dp"
        android:gravity="center"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="63dp"
        android:layout_marginEnd="89dp"
        android:gravity="center"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView4" />

    <!-- </LinearLayout>-->

</android.support.constraint.ConstraintLayout>

在底部有两个文本视图,水平放置,它们之间有空间。非常感谢:)

【问题讨论】:

  • 投反对票有什么理由吗?
  • 检查我的答案
  • 我也不懂投反对票。

标签: android android-layout android-constraintlayout


【解决方案1】:

要删除两个链式视图之间的空间,只需将以下行添加到链中的第一项:

app:layout_constraintHorizontal_chainStyle="packed"

要了解更多关于链样式的信息:

【讨论】:

    【解决方案2】:

    你必须添加Guideline

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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:layout_editor_absoluteY="81dp">
    
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="23dp"
            android:layout_marginStart="32dp"
            android:gravity="center"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="@+id/imageView3"
            app:layout_constraintStart_toEndOf="@+id/imageView3" />
    
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="26dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView2"
            app:srcCompat="@mipmap/ic_launcher_round" />
    
        <TextView
            android:id="@+id/textViewHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <!--<LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="1dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="60dp"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/linearLayout2">-->
    
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="25dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="70dp"
            app:layout_constraintEnd_toStartOf="@+id/textView2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="32dp"
            android:layout_marginTop="26dp"
            android:gravity="center"
            android:text="TextView"
            app:layout_constraintStart_toEndOf="@+id/imageView2"
            app:layout_constraintTop_toTopOf="@+id/imageView2" />
    
        <!-- </LinearLayout>-->
    
        <!--  <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="1dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="60dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="@id/linearLayout"
           >-->
    
    
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="30dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView"
            app:srcCompat="@mipmap/ic_launcher_round" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="17dp"
            android:layout_gravity="center"
            android:layout_marginBottom="58dp"
            android:layout_marginEnd="213dp"
            android:layout_marginTop="58dp"
            android:gravity="center"
            android:text="TextView"
            app:layout_constraintBottom_toTopOf="@+id/imageView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toBottomOf="@+id/textViewHeader" />
    
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="67dp"
        android:gravity="center"
        app:layout_constraintEnd_toStartOf="@+id/guideline1"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent" />
    
    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="63dp"
        android:gravity="center"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/guideline1" />
    
    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline1"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"/>
    
        <!-- </LinearLayout>-->
    
    </android.support.constraint.ConstraintLayout>
    

    关于 ConstraintLayout 中的指南: 表示 ConstraintLayout 的 Guideline 辅助对象的实用程序类。 Helper 对象不会显示在设备上(它们被标记为 View.GONE)并且仅用于布局目的。它们仅在 ConstraintLayout 中工作。 [...]

    更多细节,有很好的文档:

    https://developer.android.com/reference/android/support/constraint/Guideline.html

    【讨论】:

    • 非常感谢 :) textview 5 中添加了一些边距,同时作为边距开始在设计布局上移动,但即使我删除它,两个文本视图之间仍然有一些空间。 :)
    • @Pritish 我已经用约束布局更新了我的答案
    • 谢谢,你能详细说明一下指南吗?
    • @Pritish 完成。如果我帮助了你,请投票支持我的答案并检查是否更正以关闭问题。快乐编码:)
    猜你喜欢
    • 2021-12-26
    • 2019-09-19
    • 2018-09-25
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多