2016-11-15 65 views
2

我有指导60%从顶部和LinearLayout底部约束到该指南(所以我的LinearLayout将60%的屏幕)。现在我需要将CardView的一半放到LinearLayout的底部(添加截图)。Android约束中心

CardView高度是wrap_content,原因将被动态添加,所以我不知道身高,并且不能使用类似边距底部-50dp的内容。

那是什么,我有:

Thats what I have

我需要类似的东西:

enter image description here

我找不到未来像限制由中心。请问你能帮帮我吗。

回答

-1

最有效的方法是在运行时在代码中执行此操作。等待高度计算完毕,然后添加-.5 *高度的顶部边距。

4

您只需将cardview的顶部锚点设置为连接到线性布局(或指南)的底部锚点,并对底部锚点执行相同的操作。顶部和底部锚点都指向相同的目标,卡片视图将在该目标上垂直居中。此外,你可能不需要这个线性布局,这取决于你做了什么。检查ConstraintLayout中的chains functionality

enter image description here

<android.support.v7.widget.CardView 
    android:layout_width="300dp" 
    android:layout_height="200dp" 
    tools:layout_editor_absoluteX="28dp" 
    app:layout_constraintBottom_toTopOf="@+id/guideline" 
    app:layout_constraintTop_toTopOf="@+id/guideline" 
    tools:layout_editor_absoluteY="156dp" /> 

<android.support.constraint.Guideline 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/guideline" 
    android:orientation="horizontal" 
    app:layout_constraintGuide_percent="0.5" />