ConstraintLayout不包装内容

ConstraintLayout不包装内容

本文介绍了ConstraintLayout不包装内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cardview中有一个ConstraintLayout.如图所示,按钮以网格格式排列.但是由于某些原因,在按钮下方显示了空格.我要删除此空白.

I have a constraintLayout inside a cardview.Buttons are arranged in grid format as shown in picture. But for some reason whitespace is displayed below buttons. I want to remove this whitespace.

这是xml代码

             <android.support.v7.widget.CardView
        android:id="@+id/cvSpecialisations"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:cardElevation="2dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bViewAll">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <TextView
                android:id="@+id/tvSearchBySpecialisation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:text="Search by specialisation"
                android:textSize="14dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <View
                android:id="@+id/vDividerInSpecialisationCard"
                style="@style/Divider"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toTopOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvSearchBySpecialisation"
                app:layout_constraintVertical_bias="0.0" />

            <Button
                android:id="@+id/bSpecialisation1"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="88dp"
                android:layout_height="92dp"
                android:layout_marginLeft="32dp"
                android:layout_marginStart="32dp"
                android:drawablePadding="8dp"
                android:drawableTop="@mipmap/ic_launcher"
                android:fontFamily="san-serif"
                android:paddingLeft="-5dp"
                android:paddingRight="-5dp"
                android:text="Car"
                android:textAllCaps="false"
                android:theme="@style/AppTheme.WhiteButton"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="@+id/bSpecialisation2" />

            <Button
                android:id="@+id/bSpecialisation2"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="88dp"
                android:layout_height="92dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:drawablePadding="8dp"
                android:drawableTop="@mipmap/ic_launcher"
                android:fontFamily="san-serif"
                android:paddingLeft="-5dp"
                android:paddingRight="-5dp"
                android:text="Car"
                android:textAllCaps="false"
                android:theme="@style/AppTheme.WhiteButton"
                app:layout_constraintLeft_toRightOf="@+id/bSpecialisation1"
                app:layout_constraintRight_toLeftOf="@+id/bSpecialisation3"
                app:layout_constraintTop_toBottomOf="@+id/vDividerInSpecialisationCard" />

            <Button
                android:id="@+id/bSpecialisation3"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="88dp"
                android:layout_height="92dp"
                android:layout_marginEnd="32dp"
                android:layout_marginRight="32dp"
                android:drawablePadding="8dp"
                android:drawableTop="@mipmap/ic_launcher"
                android:fontFamily="san-serif"
                android:paddingLeft="-5dp"
                android:paddingRight="-5dp"
                android:text="Car"
                android:textAllCaps="false"
                android:theme="@style/AppTheme.WhiteButton"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="@+id/bSpecialisation2" />

            <Button
                android:id="@+id/bSpecialisation4"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="88dp"
                android:layout_height="92dp"
                android:layout_marginEnd="32dp"
                android:layout_marginRight="32dp"
                android:drawablePadding="8dp"
                android:drawableTop="@mipmap/ic_launcher"
                android:fontFamily="san-serif"
                android:paddingLeft="-5dp"
                android:paddingRight="-5dp"
                android:text="Car"
                android:textAllCaps="false"
                android:theme="@style/AppTheme.WhiteButton"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="@+id/bSpecialisation5" />

            <Button
                android:id="@+id/bSpecialisation5"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="88dp"
                android:layout_height="92dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="14dp"
                android:drawablePadding="8dp"
                android:drawableTop="@mipmap/ic_launcher"
                android:fontFamily="san-serif"
                android:paddingLeft="-5dp"
                android:paddingRight="-5dp"
                android:text="Car"
                android:textAllCaps="false"
                android:theme="@style/AppTheme.WhiteButton"
                app:layout_constraintLeft_toRightOf="@+id/bSpecialisation6"
                app:layout_constraintRight_toLeftOf="@+id/bSpecialisation4"
                app:layout_constraintTop_toBottomOf="@+id/bSpecialisation2" />

            <Button
                android:id="@+id/bSpecialisation6"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="88dp"
                android:layout_height="92dp"
                android:layout_marginLeft="32dp"
                android:layout_marginStart="32dp"
                android:drawablePadding="8dp"
                android:drawableTop="@mipmap/ic_launcher"
                android:fontFamily="san-serif"
                android:paddingLeft="-5dp"
                android:paddingRight="-5dp"
                android:text="Car"
                android:textAllCaps="false"
                android:theme="@style/AppTheme.WhiteButton"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="@+id/bSpecialisation5" />


        </android.support.constraint.ConstraintLayout>

    </android.support.v7.widget.CardView>

我尝试将宽度和高度更改为match_parent wrap_content

I have tried changing width and height to match_parent wrap_content

推荐答案

您的CardView不会包装内容,因为所有垂直元素均应打包"放置.

Your CardView doesn't wrap content because of all the vertical elements should be placed "packed" chain.

所以我添加了app:layout_constraintVertical_chainStyle="packed"和一些附加的垂直边距:

So I've added app:layout_constraintVertical_chainStyle="packed" and some additional vertical margins:

<android.support.v7.widget.CardView
    android:id="@+id/cvSpecialisations"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:cardElevation="2dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/bViewAll" >

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvSearchBySpecialisation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:text="Search by specialisation"
            android:textSize="14dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/vDividerInSpecialisationCard"
            app:layout_constraintVertical_chainStyle="packed"/>

        <View
            android:id="@+id/vDividerInSpecialisationCard"
            style="@style/Divider"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintTop_toBottomOf="@+id/tvSearchBySpecialisation"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/bSpecialisation2"/>

        <Button
            android:id="@+id/bSpecialisation1"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="88dp"
            android:layout_height="92dp"
            android:layout_marginLeft="32dp"
            android:drawablePadding="8dp"
            android:drawableTop="@mipmap/ic_launcher"
            android:fontFamily="san-serif"
            android:paddingLeft="-5dp"
            android:paddingRight="-5dp"
            android:text="Car"
            android:textAllCaps="false"
            android:theme="@style/AppTheme.WhiteButton"
            app:layout_constraintTop_toTopOf="@+id/bSpecialisation2"
            app:layout_constraintLeft_toLeftOf="parent" />

        <Button
            android:id="@+id/bSpecialisation2"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="88dp"
            android:layout_height="92dp"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:drawablePadding="8dp"
            android:drawableTop="@mipmap/ic_launcher"
            android:fontFamily="san-serif"
            android:paddingLeft="-5dp"
            android:paddingRight="-5dp"
            android:text="Car"
            android:textAllCaps="false"
            android:theme="@style/AppTheme.WhiteButton"
            app:layout_constraintTop_toBottomOf="@+id/vDividerInSpecialisationCard"
            app:layout_constraintLeft_toRightOf="@+id/bSpecialisation1"
            app:layout_constraintRight_toLeftOf="@+id/bSpecialisation3"
            app:layout_constraintBottom_toTopOf="@+id/bSpecialisation5" />

        <Button
            android:id="@+id/bSpecialisation3"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="88dp"
            android:layout_height="92dp"
            android:layout_marginRight="32dp"
            android:drawablePadding="8dp"
            android:drawableTop="@mipmap/ic_launcher"
            android:fontFamily="san-serif"
            android:paddingLeft="-5dp"
            android:paddingRight="-5dp"
            android:text="Car"
            android:textAllCaps="false"
            android:theme="@style/AppTheme.WhiteButton"
            app:layout_constraintTop_toTopOf="@+id/bSpecialisation2"
            app:layout_constraintRight_toRightOf="parent" />

        <Button
            android:id="@+id/bSpecialisation4"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="88dp"
            android:layout_height="92dp"
            android:layout_marginEnd="32dp"
            android:layout_marginRight="32dp"
            android:drawablePadding="8dp"
            android:drawableTop="@mipmap/ic_launcher"
            android:fontFamily="san-serif"
            android:paddingLeft="-5dp"
            android:paddingRight="-5dp"
            android:text="Car"
            android:textAllCaps="false"
            android:theme="@style/AppTheme.WhiteButton"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/bSpecialisation5" />

        <Button
            android:id="@+id/bSpecialisation5"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="88dp"
            android:layout_height="92dp"
            android:layout_marginTop="14dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:drawablePadding="8dp"
            android:drawableTop="@mipmap/ic_launcher"
            android:fontFamily="san-serif"
            android:paddingLeft="-5dp"
            android:paddingRight="-5dp"
            android:text="Car"
            android:textAllCaps="false"
            android:theme="@style/AppTheme.WhiteButton"
            app:layout_constraintTop_toBottomOf="@+id/bSpecialisation2"
            app:layout_constraintLeft_toRightOf="@+id/bSpecialisation6"
            app:layout_constraintRight_toLeftOf="@+id/bSpecialisation4"
            app:layout_constraintBottom_toBottomOf="parent"/>

        <Button
            android:id="@+id/bSpecialisation6"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="88dp"
            android:layout_height="92dp"
            android:layout_marginLeft="32dp"
            android:drawablePadding="8dp"
            android:drawableTop="@mipmap/ic_launcher"
            android:fontFamily="san-serif"
            android:paddingLeft="-5dp"
            android:paddingRight="-5dp"
            android:text="Car"
            android:textAllCaps="false"
            android:theme="@style/AppTheme.WhiteButton"
            app:layout_constraintTop_toTopOf="@+id/bSpecialisation5"
            app:layout_constraintLeft_toLeftOf="parent" />

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

它应该像这样:

这篇关于ConstraintLayout不包装内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:23