【问题标题】:Android. ConstraintLayout: TextView is not constraint, margin not working安卓。 ConstraintLayout:TextView 不受约束,边距不起作用
【发布时间】:2021-10-05 16:08:31
【问题描述】:

我正在尝试使用约束流。我需要在屏幕上一个接一个地排列TextView。 这是我的代码:

<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:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[AAAAAAAA]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[BBBBBBBB]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[CCCCCCCC]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[DDDDDDDD]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[EEEEEEEE]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF]"
        tools:ignore="MissingConstraints" />

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:constraint_referenced_ids="text1,text2,text3,text4,text5,text6"
        app:flow_horizontalBias="0"
        app:flow_horizontalGap="10dp"
        app:flow_horizontalStyle="packed"
        android:layout_marginEnd="20dp"
        app:flow_verticalBias="0"
        app:flow_wrapMode="chain"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_goneMarginEnd="20dp" />


 </androidx.constraintlayout.widget.ConstraintLayout>

除了最后一个 TextView 之外,一切都运行良好。由于文字很长,有些文字会从屏幕上消失(见下图)。我正在尝试设置边距,但它不起作用。 为了清楚起见,我在 xml 中添加了 TextView。我需要在运行时动态添加它们,所以我无法将 TextView 的宽度设置为 match_parent,因为我不知道内容是否适合屏幕。 请帮我。无论 TextView 中文本的长度如何,我都需要在右侧留出一个边距。

注意:TextViews 也可以动态添加。 我需要在运行时添加TextView。因此,我无法在运行时将TextView的宽度设置为match_parent,因为我不知道文本是否适合屏幕宽度。

这是我以编程方式添加 View 的代码:

  private fun addViews() {
        val list = generateChipButtons()

        list.forEach { button ->
            val view = LayoutInflater.from(baseContext).inflate(R.layout.chip_view, constraintContainer, false).apply {
                id = View.generateViewId()
                this.findViewById<TextView>(R.id.tvChipText).text = button.text
            }
            constraintContainer.addView(view)
            flowView.addView(view)
        }
    }

【问题讨论】:

  • 显示动态添加 TextView 的 Java 或 Kotlin 代码。
  • @Sniffer,请查看编辑
  • 检查this
  • @Sniffer,如果我将 TextView 的宽度设置为 match_parent,它将不完全正确。由于我的视图文本可以包含短文本,在这种情况下,其他视图可以适合该行,但是,如果我设置了 match_parent ,那么其他视图将不再适合。
  • 这取决于你。您必须根据需要设计视图

标签: android textview constraints margin android-constraintlayout


【解决方案1】:

match_parent 用于TextView text6。试试下面的代码

<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:orientation="vertical">

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[AAAAAAAA]"
    tools:ignore="MissingConstraints" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[BBBBBBBB]"
    tools:ignore="MissingConstraints" />

<TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[CCCCCCCC]"
    tools:ignore="MissingConstraints" />

<TextView
    android:id="@+id/text4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[DDDDDDDD]"
    tools:ignore="MissingConstraints" />

<TextView
    android:id="@+id/text5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[EEEEEEEE]"
    tools:ignore="MissingConstraints" />

<TextView
    android:id="@+id/text6"
    android:layout_width="match_parent"

    android:layout_height="wrap_content"
    android:text="[FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF]"
    tools:ignore="MissingConstraints" />

<androidx.constraintlayout.helper.widget.Flow
    android:id="@+id/flow1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"

    android:padding="10dp"
    app:constraint_referenced_ids="text1,text2,text3,text4,text5,text6"
    app:flow_horizontalBias="0"
    app:flow_horizontalGap="10dp"
    app:flow_horizontalStyle="packed"
    app:flow_verticalBias="0"
    app:flow_wrapMode="chain"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_goneMarginEnd="20dp" />


   </androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 感谢您的回答。在这种情况下它可以工作,但是如果文本视图是动态添加的,如何使它工作?
  • 您必须根据需要使用宽度作为 match_parent 或 wrap_content。见this
【解决方案2】:

0dp 设置为宽度并添加对比度以设置宽度的大小比例。上面的代码用wrap_content显示,宽度超过了文字大小。

<TextView
        android:id="@+id/text6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="[FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF]"
        tools:ignore="MissingConstraints"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

【讨论】:

  • 感谢您的回复。请查看我的问题中的注释。
【解决方案3】:

只需设置textView@+id/text6的宽度ma​​tch_parent

<TextView
        android:id="@+id/text6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="[FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFaslkfjadflskjflasdkjfsdlajkfasdlfjkasdlfjkaslkdfjasdflkjasdfasdlfjksdafFFFFsdflkfdaslkfjsdafkljsdlafkjdasfljk]"
        tools:ignore="MissingConstraints" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多