1

尊重限制我试图把一个ConstraintLayout内的CardView,对双方一定的余量。CardView在Android不从ConstraintLayout

我设置CardView利润率相比,父母,左,右,然后设置layout_width在0dp:它会延长,并采取一切可用的空间,忽略了利润率。

如果我将CardView放入FrameLayout中,然后将相同的确切约束应用于FrameLayout,则FrameLayout将尊重它们,并且CardView将适合它(请参阅下面的代码)。

<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="...MyActivity..."> 
    <FrameLayout 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="32dp" 
     android:layout_marginLeft="32dp" 
     android:layout_marginRight="32dp" 
     android:layout_marginStart="32dp" 
     android:layout_marginTop="32dp" 
     app:layout_constraintBottom_toTopOf="@+id/nextButton" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"> 

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

所以我得到它的工作我需要的方式,但我想明白为什么计算自己的宽度,设置为0dp时CardView不走的利润考虑在内。

回答

0

在CardView从两侧设置距离,你可能会看到这个例子:

<android.support.v7.widget.CardView 
    android:layout_width="0dp" 
    android:layout_height="186dp" 
    android:layout_marginLeft="12dp" 
    android:layout_marginRight="12dp" 
    android:layout_marginTop="8dp" 
    android:alpha=".5" 
    android:clickable="true" 
    android:foreground="?android:attr/selectableItemBackground" 
    app:cardBackgroundColor="@android:color/black" 
    app:cardCornerRadius="4dp" 
    app:constraintSet="@dimen/spacing_normal" 
    app:contentPaddingBottom="@dimen/spacing_double" 
    app:contentPaddingTop="@dimen/spacing_double" 
    app:elevation="@dimen/cardview_default_elevation" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    tools:layout_constraintLeft_creator="1" 
    tools:layout_constraintRight_creator="1" 
    tools:layout_constraintTop_creator="1" /> 

只是让我知道结果

+0

不会只改变编辑器内的位置吗?这是我对“工具”命名空间的理解 – gianpi

+0

我编辑了我的文章,因为我不得不添加一些东西,它的工作原理。 – Meikiem

+0

检查一下,我测试了它,左右边距保持不变 – Meikiem

0

它不工作的原因是,它需要layout_marginEnd和layout_marginStart而不是layout_marginLeft和layout_marginRight。

这些似乎默认添加到FrameLayout但不CardView。