簡體   English   中英

在 ConstraintLayout 中將 ImageView 與 TextView 對齊

[英]Align ImageView with TextView in a ConstraintLayout

問題圖像測試

我有這個組合,我正在測試鏈的使用,但我有一個疑問,那樣的話,使用約束布局,我可以將上面的 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>

將 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" />

它應該像你描述的那樣工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM