2017-01-26 9 views
4

私はむしろ単純なレイアウトを持っています実際には半分ではありませんが、あなたはポイントを得る)。センター固定サイズのImageViewの

しかし、ImageViewは親の左側に付いていますが、私はそれを水平にセンタリングする方法はわかりません - Googleで見つけることができるのは、複数の要素のチェーンですが、 。

レイアウトエディタは私にとって非常に悪いです。私の最初の苦情は、私が親に制約を作成することができないように見えるということです。次に、親の中心にあるボタンを押して、ImageViewを親の中央に配置してみました。これにより、CardViewの幅が増加し、ImageViewにはまったく影響しません。

CardViewの上にImageViewをzオーダーするにはどうすればよいですか?ここで

レイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.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="match_parent" 
    android:background="@color/colorPrimaryDark"> 
<android.support.v7.widget.CardView 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_marginBottom="32dp" 
    android:layout_marginEnd="32dp" 
    android:layout_marginStart="32dp" 
    android:layout_marginTop="160dp" 
    app:cardCornerRadius="12dp" 
    app:cardElevation="32dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintTop_toTopOf="parent"> 

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

<ImageView 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_marginTop="40dp" 
    android:scaleType="centerCrop" 
    android:src="@drawable/re_zero" 

    app:layout_constraintHorizontal_bias="0.5" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 

ありがとう! docsによると

答えて

8

ビューの両側に制約(と同じ次元の表示サイズ が「固定」または「コンテンツをラップ」されるか)、ビュー を追加デフォルトでは2つのアンカーポイントの中央に配置されます。

ImageViewを中心とする次の制約を追加します。

また
app:layout_constraintEnd_toEndOf="parent" 

、標高がzインデックスを上書きするのでCardViewは、z屈折率を修正よりも高い標高を加えます。

+0

素晴らしいソリューション!これを理解することにいくつか問題があった – hcpl

関連する問題