【问题标题】:How to place text view on right side of image view without overlapping it and covering whole width in constraint layout如何将文本视图放置在图像视图的右侧而不重叠并在约束布局中覆盖整个宽度
【发布时间】:2021-12-16 03:37:03
【问题描述】:

我正在尝试将文本视图放置在图像视图的右侧。虽然我能够做到这一点,但当我将匹配父级提供给文本视图时,我遇到了问题。我希望文本覆盖剩余区域,但是当我使用匹配父级时,它会与左侧的图像视图重叠。 这是我的代码

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/margin_default"
    android:layout_marginTop="12dp"

    android:layout_marginEnd="@dimen/margin_default"
    android:layout_marginBottom="12dp">


    <ImageView
        android:id="@+id/effective_warnings_card_icon"
        android:layout_width="48dp"
        android:layout_height="0dp"
        android:focusable="false"

        android:importantForAccessibility="no"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="@id/effective_warnings_card_text"
        app:srcCompat="@drawable/account_unavailable_image"
        tools:src="@tools:sample/avatars" />

    <TextView
        android:id="@+id/effective_warnings_card_text"
        style="@style/TextAppearance.TSBApp20.Body.Regular"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:text="aggfgffh"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toRightOf="@+id/effective_warnings_card_icon"
        app:layout_constraintStart_toEndOf="@+id/effective_warnings_card_icon"
        app:layout_constraintTop_toTopOf="parent" />


    <!--            <ImageView-->
    <!--                android:id="@+id/effective_warnings_card_icon"-->

    <!--                        android:layout_width="48dp"-->
    <!--                        android:layout_height="48dp"-->
    <!--                        android:layout_marginStart="@dimen/margin_default"-->
    <!--                        app:srcCompat="@drawable/account_unavailable_image"-->
    <!--                        android:importantForAccessibility="no"-->
    <!--                        android:focusable="false"-->
    <!--                        app:layout_constraintTop_toTopOf="parent"-->
    <!--                        app:layout_constraintBottom_toBottomOf="parent"-->
    <!--                        app:layout_constraintStart_toStartOf="parent"-->
    <!--                        tools:src="@tools:sample/avatars"-->
    <!--                            />-->
    <!--            <TextView-->
    <!--                android:id="@+id/effective_warnings_card_text"-->
    <!--                style="@style/TextAppearance.TSBApp20.Body.Regular"-->
    <!--                android:layout_width="wrap_content"-->
    <!--                android:layout_height="wrap_content"-->
    <!--android:text="afafafaffafa"-->
    <!--                app:layout_constraintEnd_toEndOf="parent"-->
    <!--                app:layout_constraintStart_toEndOf="@+id/effective_warnings_card_icon"-->
    <!--                app:layout_constraintBottom_toBottomOf="@+id/effective_warnings_card_icon"-->
    <!--                app:layout_constraintTop_toTopOf="@+id/effective_warnings_card_icon"-->
    <!--                />-->


</androidx.constraintlayout.widget.ConstraintLayout>

在添加的图像中,您可以看到以黑色布局覆盖图像的文本。如何做到这一点?

【问题讨论】:

  • 分享您想要实现的 UI。
  • @MehulKabaria 这是在其他地方使用的卡片布局的一部分,因此无法分享整个内容。基本上我需要并排的图像和文本,比如图像右侧的文本,整体在卡片视图中。并在适配器中使用它。

标签: android android-constraintlayout


【解决方案1】:

您必须将0dp 设置为您的TextView

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/margin_default"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="@dimen/margin_default"
    android:layout_marginBottom="12dp">

    <ImageView
            android:id="@+id/effective_warnings_card_icon"
            android:layout_width="48dp"
            android:layout_height="0dp"
            android:focusable="false"

            android:importantForAccessibility="no"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="@id/effective_warnings_card_text"
            app:srcCompat="@drawable/account_unavailable_image"
            tools:src="@tools:sample/avatars" />

        <TextView
            android:id="@+id/effective_warnings_card_text"
            style="@style/TextAppearance.TSBApp20.Body.Regular"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:text="aggfgffh"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/effective_warnings_card_icon"
            app:layout_constraintStart_toEndOf="@+id/effective_warnings_card_icon"
            app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    【解决方案2】:

    TextView 中删除app:layout_constraintLeft_toRightOf="@+id/effective_warnings_card_icon"

    同时从ImageView 中删除app:layout_constraintEnd_toEndOf="@id/effective_warnings_card_text"

    0dp宽度设置为TextView

        <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_default"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="@dimen/margin_default"
        android:layout_marginBottom="12dp">
    
        <ImageView
                android:id="@+id/effective_warnings_card_icon"
                android:layout_width="48dp"
                android:layout_height="0dp"
                android:focusable="false"
    
                android:importantForAccessibility="no"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/account_unavailable_image"
                tools:src="@tools:sample/avatars" />
    
            <TextView
                android:id="@+id/effective_warnings_card_text"
                style="@style/TextAppearance.TSBApp20.Body.Regular"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:text="aggfgffh"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toEndOf="@+id/effective_warnings_card_icon"
                app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-18
      • 2021-12-26
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多