Android - Constraint layout - How to align a view centered over edge of other view?

前端 未结 2 1500
北恋
北恋 2021-02-05 03:42

I want to build a layout like this:

Inside the constraint layout there is an Image View which acts like a banner, then there is a Card that is center aligned wi

相关标签:
2条回答
  • 2021-02-05 03:51

    Try this:

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20dp">
    
        <android.support.v7.widget.CardView
            android:id="@+id/card_1"
            android:layout_width="0dp"
            android:layout_height="200dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <android.support.v7.widget.CardView
            android:id="@+id/card_2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="0dp"
            app:cardBackgroundColor="#69F"
            app:layout_constraintBottom_toBottomOf="@+id/card_1"
            app:layout_constraintLeft_toLeftOf="@+id/card_1"
            app:layout_constraintRight_toRightOf="@+id/card_1"
            app:layout_constraintTop_toBottomOf="@+id/card_1" />
    
    </android.support.constraint.ConstraintLayout>
    

    Explanation :- This works because of these four lines

    Following lines sets the blue CardView centered on the bottom edge of White CardView.

    <!-- top constraint is set to bottom of White CardView -->
    app:layout_constraintTop_toBottomOf="@+id/card_1"
    
    <!-- bottom constraint is set to bottom of White CardView -->
    app:layout_constraintBottom_toBottomOf="@+id/card_1"
    

    Following lines set the blue CardView centered horizontally

    <!-- left constraint is set to left of White CardView -->
    app:layout_constraintLeft_toLeftOf="@+id/card_1"
    
    <!-- right constraint is set to right of White CardView -->
    app:layout_constraintRight_toRightOf="@+id/card_1"
    
    0 讨论(0)
  • 2021-02-05 03:51

    try below:

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_collapseMode="parallax">
    
    
        <ImageView
            android:id="@+id/imageView_jobBackdrop_jobDetails"
            android:layout_width="match_parent"
            android:layout_height="175dp"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:background="@android:color/white"
            app:layout_collapseMode="parallax"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:background="@android:color/black"
            app:layout_constraintBottom_toBottomOf="@+id/imageView_jobBackdrop_jobDetails"
            app:layout_constraintEnd_toEndOf="@+id/imageView_jobBackdrop_jobDetails"
            app:layout_constraintStart_toStartOf="@+id/imageView_jobBackdrop_jobDetails"
            app:layout_constraintTop_toBottomOf="@+id/imageView_jobBackdrop_jobDetails"  />
    
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:background="@android:color/darker_gray"
            app:layout_constraintBottom_toTopOf="@+id/imageView2"
            app:layout_constraintEnd_toEndOf="@+id/imageView2"
            app:layout_constraintStart_toStartOf="@+id/imageView2"
            app:layout_constraintTop_toTopOf="@+id/imageView2"  />
    
    
    </android.support.constraint.ConstraintLayout>
    

    0 讨论(0)
提交回复
热议问题