2016-11-15 67 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" />