ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – ConstraintLayout中的CardView没有包装高度

2019-05-22 21:24:11  阅读:321  来源: 互联网

标签:android android-cardview android-constraintlayout


ConstraintLayout中的CardView没有包装高度.

面对similar issue稳定的CL库:

compile 'com.android.support.constraint:constraint-layout:1.0.2'

Android Studio:2.3.2

观察:

>设置硬编码的cardview-height可以解决问题.
>使用RelativeLayout修复更改CardView.

是不是仍然有一个issue与Cardview,它很难找到ConstraintLayout.

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_absoluteX="0dp"
    tools:layout_editor_absoluteY="25dp">


    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="0dp"
        android:layout_height="122dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        app:cardBackgroundColor="@color/io15_blue_grey_100"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="spread">

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

            <ImageView
                android:id="@+id/mobLeftIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/mobInputLayout"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/mobInputLayout"
                app:layout_constraintTop_toTopOf="@+id/mobInputLayout"
                app:srcCompat="@drawable/ic_up" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/mobInputLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:layout_constraintLeft_toRightOf="@+id/mobLeftIcon"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <EditText
                    android:id="@+id/editText4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Mobile Number"
                    android:text="123123123" />
            </android.support.design.widget.TextInputLayout>

            <ImageView
                android:id="@+id/phoneLeftIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/phoneInputLayout"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/phoneInputLayout"
                app:layout_constraintTop_toTopOf="@+id/phoneInputLayout"
                app:srcCompat="@drawable/ic_up" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/phoneInputLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:layout_constraintLeft_toRightOf="@+id/phoneLeftIcon"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/mobInputLayout">

                <EditText
                    android:id="@+id/editText5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Telphone Number"
                    android:text="123123123" />
            </android.support.design.widget.TextInputLayout>

        </android.support.constraint.ConstraintLayout>

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

</android.support.constraint.ConstraintLayout>

截图:
enter image description here

解决方法:

要使由ConstraintLayout表示的CardView包装内容,您必须向ConstraintLayout中的视图添加3个链:

> mobLeftIcon和mobInputLayout之间的水平传播链
> phoneLeftIcon和phoneInputLayout之间的水平传播链
> mobInputLayout和phoneInputLayout之间的垂直传播链

查看布局编辑器的屏幕截图:

Chains on Layout Editor

这是您的布局的最终​​源代码:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

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

            <ImageView
                android:id="@+id/mobLeftIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginBottom="8dp"
                app:srcCompat="@drawable/ic_up"
                app:layout_constraintTop_toTopOf="@+id/mobInputLayout"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="@+id/mobInputLayout"
                app:layout_constraintEnd_toStartOf="@+id/mobInputLayout"
                app:layout_constraintHorizontal_chainStyle="spread" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/mobInputLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toEndOf="@+id/mobLeftIcon"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toTopOf="@+id/phoneInputLayout"
                app:layout_constraintVertical_chainStyle="spread">

                <EditText
                    android:id="@+id/editText4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Mobile Number"
                    android:text="123123123" />
            </android.support.design.widget.TextInputLayout>

            <ImageView
                android:id="@+id/phoneLeftIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginBottom="8dp"
                app:srcCompat="@drawable/ic_up"
                app:layout_constraintTop_toTopOf="@+id/phoneInputLayout"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="@+id/phoneInputLayout"
                app:layout_constraintEnd_toStartOf="@+id/phoneInputLayout"
                app:layout_constraintHorizontal_chainStyle="spread" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/phoneInputLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintTop_toBottomOf="@+id/mobInputLayout"
                app:layout_constraintStart_toEndOf="@+id/phoneLeftIcon"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent">

                <EditText
                    android:id="@+id/editText5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Telphone Number"
                    android:text="123123123" />

            </android.support.design.widget.TextInputLayout>

        </android.support.constraint.ConstraintLayout>

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

</android.support.constraint.ConstraintLayout>

结果,你会得到类似的东西:

Simulator screenshot

标签:android,android-cardview,android-constraintlayout
来源: https://codeday.me/bug/20190522/1154013.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有