ConstraintLayout in Cardview adding white space

故事扮演 提交于 2019-12-06 08:09:26

问题


I am trying to create a CardView that has a ConstraintLayout to organize some TextView.

Sometimes it'll display as intended, but sometimes, it'll add extra white space such as when a keyboard is closed and ruin the layout. I have a GIF below showing the CardView working and not working in the same span of time.

A code snippet for the relevant CardView is below. I use wrap_content to try to keep the CardView at the size I want, but it still expands.

    <android.support.v7.widget.CardView
    android:id="@+id/cardView_game_cash"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="6dp"
    app:cardCornerRadius="4dp"
    app:contentPadding="6dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

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

        <TextView
            android:id="@+id/textView_game_cashLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Total Cash"
            app:layout_constraintLeft_toLeftOf="@+id/textView_game_cash"
            app:layout_constraintRight_toRightOf="@+id/textView_game_cash"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView_game_totalProfitLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Total Profit"
            app:layout_constraintLeft_toLeftOf="@+id/textView_game_totalProfit"
            app:layout_constraintRight_toRightOf="@+id/textView_game_totalProfit"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView_game_profitLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Win Profit"
            app:layout_constraintLeft_toLeftOf="@+id/textView_game_profit"
            app:layout_constraintRight_toRightOf="@+id/textView_game_profit"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView_game_cash"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:gravity="center"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/textView_game_totalProfit"
            app:layout_constraintTop_toBottomOf="@+id/textView_game_cashLabel"
            tools:text="TextView" />

        <TextView
            android:id="@+id/textView_game_totalProfit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:gravity="center"
            app:layout_constraintLeft_toRightOf="@+id/textView_game_cash"
            app:layout_constraintRight_toLeftOf="@+id/textView_game_profit"
            app:layout_constraintTop_toTopOf="@+id/textView_game_cash"
            tools:text="TextView" />

        <TextView
            android:id="@+id/textView_game_profit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:gravity="center"
            app:layout_constraintLeft_toRightOf="@+id/textView_game_totalProfit"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/textView_game_cash"
            tools:text="TextView" />
    </android.support.constraint.ConstraintLayout>

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

回答1:


I found a solution to my problem. In the TextView within the ConstraintLayout, I replaced android:layout_height="wrap_content"

with:

android:layout_height="0dp" app:layout_constraintHeight_default="wrap"

The layout worked as intended with that change to all the TextView within.



来源:https://stackoverflow.com/questions/43126724/constraintlayout-in-cardview-adding-white-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!