设置高度以与约束中的约束相匹配将缩小视图的高度

时间:2019-03-20 06:40:02

标签: android xml android-constraintlayout

我在ConstraintLayout中有3张卡片布局。我想使该行中第二张和第三张纸牌的高度与第一张纸牌的高度匹配。

所以我添加了行条件TopToTop of和BottomToBottom Of。然后将高度设置为0dp(设计视图中的匹配约束)。令人惊讶的是,第二张和第三张纸牌的高度居中且不匹配。请帮助我解决这个问题。

由于某种原因,我在每种卡片布局中都得到了双阴影图像。我不确定为什么。

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/rlHeader"
            android:layout_width="0dp"
            android:layout_height="@dimen/_40dp"
            android:background="@color/action_color"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingStart="@dimen/_0dp"
            android:paddingEnd="@dimen/_10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <ImageView
                android:id="@+id/imgMenu"
                android:layout_width="@dimen/_20dp"
                android:layout_height="@dimen/_20dp"
                android:layout_centerVertical="true"
                android:layout_marginStart="@dimen/_10dp"
                android:contentDescription="@null"
                android:src="@drawable/icon_menu" />

            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_centerVertical="true"
                android:layout_marginStart="@dimen/_10dp"
                android:fontFamily="@font/proxima_nova_semibold"
                android:text="@string/pick_a_service"
                android:textColor="@color/white"
                android:textSize="@dimen/_18sp" />
        </RelativeLayout>

        <androidx.cardview.widget.CardView
            android:id="@+id/card_home1"
            style="@style/cardHome"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:cardBackgroundColor="@color/colorAccent1"
            app:layout_constraintEnd_toStartOf="@+id/card_home5"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rlHeader">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginTop="@dimen/_2dp"
                    android:layout_weight="0.9"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />

                <TextView
                    android:id="@+id/featureText1"
                    style="@style/textHome"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/_2dp"
                    android:layout_weight="0.1"
                    android:text="@string/verification" />
            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:id="@+id/card_home5"
            style="@style/cardHome"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:cardBackgroundColor="@color/colorAccent5"
            app:cardElevation="0dp"
            app:layout_constraintBottom_toBottomOf="@id/card_home1"
            app:layout_constraintEnd_toStartOf="@+id/card_home8"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/card_home1"
            app:layout_constraintTop_toTopOf="@id/card_home1">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginTop="@dimen/_2dp"
                    android:layout_weight="0.9"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />

                <TextView
                    android:id="@+id/featureText5"
                    style="@style/textHome"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/_2dp"
                    android:layout_weight="0.1"
                    android:text="@string/verification" />
            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:id="@+id/card_home8"
            style="@style/cardHome"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:cardBackgroundColor="@color/colorAccent3"
            app:layout_constraintBottom_toBottomOf="@id/card_home1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/card_home5"
            app:layout_constraintTop_toTopOf="@+id/card_home1">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginTop="@dimen/_2dp"
                    android:layout_weight="0.9"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />

                <TextView
                    android:id="@+id/featureText8"
                    style="@style/textHome"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/_2dp"
                    android:layout_weight="0.1"
                    android:text="@string/verification" />
            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <RelativeLayout
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone">

            <ProgressBar
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="@dimen/_50dp"
                android:layout_height="@dimen/_50dp"
                android:layout_centerInParent="true"
                android:background="@drawable/progress_dialog_back"
                android:indeterminate="false"
                android:indeterminateTint="@color/white"
                android:max="100"
                android:padding="@dimen/_5dp"
                android:progress="0"
                android:visibility="visible"
                tools:targetApi="lollipop" />

        </RelativeLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

2 个答案:

答案 0 :(得分:0)

下面是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:context=".MainActivity">



            <android.support.v7.widget.CardView
                android:id="@+id/label_a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
             android:layout_marginTop="10dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/label_b"
                app:layout_constraintTop_toTopOf="parent"
                app:cardCornerRadius="10dp"
                app:cardBackgroundColor="@color/colorAccent">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.9"
                        android:scaleType="fitCenter"
                        android:src="@mipmap/ic_launcher" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:layout_weight="0.1"
                        android:text="Verification" />
                </LinearLayout>


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

            <android.support.v7.widget.CardView
                android:id="@+id/label_b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                app:layout_constraintStart_toEndOf="@id/label_a"
                app:layout_constraintEnd_toStartOf="@+id/label_c"
                app:layout_constraintTop_toTopOf="parent"
                app:cardCornerRadius="10dp"
                app:cardBackgroundColor="@color/colorPrimaryDark"
             >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.9"
                        android:scaleType="fitCenter"
                        android:src="@mipmap/ic_launcher" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:layout_weight="0.1"
                        android:text="Verification" />
                </LinearLayout>


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

            <android.support.v7.widget.CardView
                android:id="@+id/label_c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"

                app:layout_constraintStart_toEndOf="@id/label_b"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:cardCornerRadius="10dp"
                app:cardBackgroundColor="@color/colorPrimary"
               >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.9"
                        android:scaleType="fitCenter"
                        android:src="@mipmap/ic_launcher" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:layout_weight="0.1"
                        android:text="Verification" />
                </LinearLayout>


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




    </android.support.constraint.ConstraintLayout>

Image for reference

答案 1 :(得分:0)

ConstaintLayout并不意味着具有内部视图组。其全部内容涉及平面视图层次结构-因此您可以避免使用所有内部视图组,而只需使用chains

  <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="@+id/button2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button3"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button2" />
</androidx.constraintlayout.widget.ConstraintLayout>