【问题标题】:Avoid overlap Android Constraint Layout避免重叠Android约束布局
【发布时间】:2020-12-14 13:19:29
【问题描述】:

我想避免将“状态”写成两行,我正在使用约束布局。我查看了其他类似的问题,但没有找到解决问题的方法。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?><!--
  As recommended by Google, we use a ConstraintLayout
  as the root element
  We add a padding all around using the padding attribute
-->
<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"
    android:padding="20dp"
    >

    <!--
    * We use the hint attribute to display the "type" string
    * The type string has been extracted into a dedicated file
    * The width is "0dp" because the "match_parent" value cannot be
    used with the constraintLayout
    * We use the inputType attribute to display the best keyboard
    to the user
    *
    -->

    <TextView
        android:id="@+id/status_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/price"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@id/status"
        android:text="Status :"
        android:textStyle=""
        android:textSize="18sp"
        />

    <Spinner
        android:id="@+id/status"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/status_label"
        app:layout_constraintTop_toTopOf="@id/status_label"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

这是视图: View

希望您能提供一些解决方案。

【问题讨论】:

  • 将你的文本视图和微调器包裹在线性布局中,并相应地设置填充/边距。
  • 可以将status_label的宽度设为wrap_content
  • 如果你只是删除 app 也可能有效:layout_constraintEnd_toStartOf="@id/status"
  • @KrishnaSharma 是的,它也有效

标签: android xml android-constraintlayout overlap


【解决方案1】:

您可以将status_label的宽度设置为wrap_content

【讨论】:

  • 如果你接受这个答案,那就太好了,这样其他人也能从中得到帮助。
【解决方案2】:
<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"
android:padding="20dp"
>

<!--
* We use the hint attribute to display the "type" string
* The type string has been extracted into a dedicated file
* The width is "0dp" because the "match_parent" value cannot be
used with the constraintLayout
* We use the inputType attribute to display the best keyboard
to the user
*
-->

<TextView
    android:id="@+id/status_label"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
  
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toStartOf="@id/status"
    android:text="Status :"
    android:textStyle=""
    android:textSize="18sp"
    />

<Spinner
    android:id="@+id/status"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toEndOf="@id/status_label"
    app:layout_constraintTop_toBottomOf="@id/status_label"
    />

还有这里

【讨论】:

  • 它不起作用,状态文本视图只是向下移动了一点,但重叠没有任何变化
猜你喜欢
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
相关资源
最近更新 更多