gpt4 book ai didi

Android - 约束布局 - 如何将 View 居中对齐到其他 View 的边缘?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:54:17 24 4
gpt4 key购买 nike

我想构建这样的布局:

enter image description here

在约束布局中有一个像横幅一样的 ImageView ,然后是一个与横幅底部边缘居中对齐的卡片,然后是另一个与横幅顶部边缘居中对齐的 ImageView 卡片。

我面临的问题是第二个 Image View(绿色)在与卡片对齐时进入背景而不是留在前景。

这是xml,

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
android:paddingBottom="@dimen/padding_10">

<ImageView
android:id="@+id/imageView_jobBackdrop_jobDetails"
android:layout_width="match_parent"
android:layout_height="175dp"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/backdrop_job_details"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_collapseMode="parallax"/>

<ImageView
android:id="@+id/imageView_companyLogo_jobDetails"
android:layout_width="75dp"
android:layout_height="75dp"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
app:layout_constraintBottom_toTopOf="@+id/cardView_jobHeader_jobDetails"
app:layout_constraintEnd_toEndOf="@id/cardView_jobHeader_jobDetails"
app:layout_constraintStart_toStartOf="@id/cardView_jobHeader_jobDetails"
app:layout_constraintTop_toTopOf="@+id/cardView_jobHeader_jobDetails" />

<android.support.v7.widget.CardView
android:id="@+id/cardView_jobHeader_jobDetails"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/imageView_jobBackdrop_jobDetails"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView_jobBackdrop_jobDetails"
app:layout_constraintVertical_bias="0.5">

<android.support.constraint.ConstraintLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textView_jobTitle_jobDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:gravity="center"
android:text="Fresher Software Developer Job. Urgent Openning. Angular Js, HTML, JavaScript, CSS"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

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

最佳答案

试试这个:

<?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_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="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_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="0dp"
app:cardBackgroundColor="#69F"
app:layout_constraintBottom_toBottomOf="@+id/card_1"
app:layout_constraintStart_toStartOf="@+id/card_1"
app:layout_constraintEnd_toEndOf="@+id/card_1"
app:layout_constraintTop_toBottomOf="@+id/card_1" />

</android.support.constraint.ConstraintLayout>

See this image

解释 :- 这行是因为这四行

以下几行代码将蓝色 CardView 设置为以白色 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"

以下几行设置蓝色 CardView 水平居中

<!-- left/start constraint is set to left/start of White CardView -->
app:layout_constraintStart_toStartOf="@+id/card_1"

<!-- right/end constraint is set to right/end of White CardView -->
app:layout_constraintEnd_toEndOf="@+id/card_1"

关于Android - 约束布局 - 如何将 View 居中对齐到其他 View 的边缘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46645517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com