Android ConstraintWidth_%在设计选项卡和实践中看起来不同

Android ConstraintWidth_%在设计选项卡和实践中看起来不同,android,android-layout,android-xml,android-constraintlayout,Android,Android Layout,Android Xml,Android Constraintlayout,我正在尝试在我的shop\u list\u item.xml中使用ConstraintWidth\u percent,它在我的shopadapter中使用。我遇到的问题是,“设计”选项卡(外观)和“应用程序内设计”(外观)完全不同。我做错了什么 应该是什么样子 看起来怎么样 代码 当我将constraintWidth_percent更改为非常高的值(如0.8)时,它会正常工作(但在“设计”选项卡中看起来很奇怪)。这是一种正常行为,因为Android Studio的设计预览的屏幕大小可能与

我正在尝试在我的
shop\u list\u item.xml中使用
ConstraintWidth\u percent
,它在我的shopadapter中使用。我遇到的问题是,“设计”选项卡(外观)和“应用程序内设计”(外观)完全不同。我做错了什么

应该是什么样子

看起来怎么样

代码


当我将constraintWidth_percent更改为非常高的值(如0.8)时,它会正常工作(但在“设计”选项卡中看起来很奇怪)。

这是一种正常行为,因为Android Studio的设计预览的屏幕大小可能与您的移动设备或模拟器的屏幕大小不同。。您可以更改设计预览的宽度/高度,使其具有与测试模拟器/手机类似的宽度/高度,您会注意到没有任何更改

您可以更改此表单:

这一点非常明显,因为您的
cardwiew
显然占据了
RecyclerView
项目宽度的40%。您可以注意到这将在下图中显示绿色指南

你可以做的是柚木的40%,直到你觉得舒适的某个宽度,可以适合的购物车项目

最后一件事,你可以尝试下面的布局来排列项目在代码的中间>回收版> /代码>项目布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="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.4">


        <!--        Add CardView items -->


    </com.google.android.material.card.MaterialCardView>

    <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.3" />


</androidx.constraintlayout.widget.ConstraintLayout>


CardView的宽度约束告诉您希望宽度与父项相同,然后您使用'app:layout\u constraintWidth\u percent=“0.40”`来拥有父项的40%。。这可能有点过分了我不太明白你的意思。你的意思是组合
layout\u width=“0dp”
app:layout\u constraintWidth\u percent=“0.4”
?我看的每一个教程都说我必须将布局宽度设置为0dp,以实现约束宽度百分比。那我怎么解决呢?完全正确,但您也设置了
app:layou constraintStart\u toStartOf=“parent”
&
app:layou constraintEnd\u toEndOf=“parent”
。。这两个设置了
cardwiew
宽度以匹配父级宽度。我已经删除了这两个宽度,但没有任何更改,而是添加了“此视图不是水平约束”,现在它处于约束之下,因为您需要
cardwiew
宽度为父级的40%。。但没有告诉它从父母的哪一点可以消费40%。。例如,它是从父级开始的吗
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="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.4">


        <!--        Add CardView items -->


    </com.google.android.material.card.MaterialCardView>

    <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.3" />


</androidx.constraintlayout.widget.ConstraintLayout>