【问题标题】:Align ImageView with TextView in a ConstraintLayout在 ConstraintLayout 中将 ImageView 与 TextView 对齐
【发布时间】:2020-03-25 05:02:15
【问题描述】:

我有这个组合,我正在测试链的使用,但我有疑问,这样,使用约束布局,我可以将上面的 TextView 与下面的 ImageView 对齐吗?没有上面的 TextView 从它的位置移动。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/test__label__text_top"
        android:layout_width="180dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@id/test__label__text_bot"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        tools:text="Texto superior" />

    <ImageView
        android:id="@+id/test__img__image_view"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginTop="10dp"
        app:layout_constraintEnd_toStartOf="@+id/test__label__text_bot"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/test__label__text_top"
        tools:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/test__label__text_bot"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/test__img__image_view"
        app:layout_constraintTop_toBottomOf="@+id/test__label__text_top"
        tools:text="Texto inferior" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android constraints android-constraintlayout


    【解决方案1】:

    将 ImageView 约束更改为:

    <ImageView
        android:id="@+id/test__img__image_view"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toStartOf="@+id/test__label__text_top"
        app:layout_constraintTop_toBottomOf="@+id/test__label__text_top"
        tools:src="@mipmap/ic_launcher" />
    

    它应该像你描述的那样工作。

    【讨论】:

    • 你是对的。你说得对。我把链条弄得一团糟,我不记得即使我有一条垂直的链条,我仍然可以水平放置元素。谢谢。
    猜你喜欢
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2013-03-26
    • 2019-09-30
    • 1970-01-01
    • 2014-10-15
    • 2018-06-30
    相关资源
    最近更新 更多