【发布时间】:2021-04-15 19:53:23
【问题描述】:
我正在尝试在我的 shop_list_item.xml 中使用 ConstraintWidth_percent,它在我的 shopadapter 中使用。我遇到的问题是,设计选项卡(它应该是什么样子)和应用内设计(它看起来如何)完全不同。我在这里做错了什么?
它应该是什么样子
外观如何
代码
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.card.MaterialCardView
android:id="@+id/mcv_product_item"
android:layout_width="0dp"
android:layout_height="210dp"
android:clickable="true"
android:focusable="true"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.40">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_product_image"
android:layout_width="match_parent"
android:layout_height="110dp"
android:contentDescription="TODO"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:loadImage="@{product.images[0]}"
tools:ignore="ContentDescription, HardcodedText"
tools:src="@drawable/ic_calibrate" />
<ImageView
android:id="@+id/iv_service_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:contentDescription="@null"
android:src="@drawable/ic_service"
app:hideView="@{product.hasService}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tv_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="2"
android:text="@{product.name}"
android:textAlignment="textStart"
android:textColor="@color/color_text_dark"
android:textSize="@dimen/textDescriptionNormal1"
app:layout_constraintEnd_toEndOf="@+id/iv_product_image"
app:layout_constraintStart_toStartOf="@+id/iv_product_image"
app:layout_constraintTop_toBottomOf="@+id/iv_product_image"
tools:text="TEST TITLE TO ENSURE STUFF" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tv_product_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="@{product.price}"
android:textColor="@color/color_text_blue"
android:textSize="@dimen/textHeadlineNormal1"
android:textStyle="italic|bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/tv_product_name"
tools:text="4870.00" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tv_euro"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/currency"
android:textColor="@color/color_text_blue"
android:textSize="@dimen/textHeadlineNormal1"
android:textStyle="italic|bold"
app:layout_constraintBottom_toBottomOf="@+id/tv_product_price"
app:layout_constraintEnd_toEndOf="@+id/tv_product_name"
app:layout_constraintStart_toEndOf="@+id/tv_product_price"
app:layout_constraintTop_toTopOf="@+id/tv_product_price" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<!-- Currently not using -->
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.30"/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.70"/>
</androidx.constraintlayout.widget.ConstraintLayout>
当我将 constraintWidth_percent 更改为 0.8 等非常高的值时,它会正常工作(但在设计选项卡中看起来很奇怪)。
【问题讨论】:
-
CardView的宽度约束告诉您希望宽度与父级相同,然后您使用 `app:layout_constraintWidth_percent="0.40"` 获得父级的 40% ..这可能是过度约束 -
我不完全明白你的意思。您是指
layout_width="0dp"和app:layout_constraintWidth_percent="0.4"的组合吗?我看过的每个教程都说我必须将 layout_width 设置为 0dp 才能实现 constraintwidth_percent。那我该如何解决呢? -
绝对正确,但您还设置了
app:layout_constraintStart_toStartOf="parent"&app:layout_constraintEnd_toEndOf="parent".. 这两个设置了cardView宽度以匹配父宽度 -
我都删除了,没有任何改变,但添加了“此视图不受水平约束”
-
现在它受到约束,因为您需要
CardView宽度为父级的 40% .. 但没有告诉它从父级中的哪个点可以消耗 40% ..实例是从父节点开始的
标签: android android-layout android-xml android-constraintlayout