【问题标题】:Android custom ConstraintLayout cannot update child height when ConstraintLayout height change当ConstraintLayout高度改变时Android自定义ConstraintLayout不能更新子高度
【发布时间】:2020-11-30 07:04:45
【问题描述】:

我创建了一个自定义视图,正在扩展 ConstraintLayout,然后添加四个视图作为边框。 当我在 Activity xml 中更新子项高度时,尽管我已经将视图约束顶部、开始、结束和底部设置为父项,但四个视图无法更新高度。

但是,如果我将四个视图设置为活动 xml,而不是在自定义视图中。它可以在父视图高度更新时更新高度。

感谢您的帮助。

下面是代码:

自定义视图

class StoryComponentFrame @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0

) : ConstraintLayout(context, attrs, defStyleAttr) {

init {

  LayoutInflater.from(context).inflate(R.layout.layout_story_component_frame, this, true)

    attrs?.let { it ->
        val typedArray = context.obtainStyledAttributes(
            it,
            R.styleable.StoryComponentFrame,
            0,
            0
        )

        typedArray.recycle()
    }
}

}

自定义视图 xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <View
        android:id="@+id/v_line_start"
        android:layout_width="2dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@color/color_f05a32" />

    <View
        android:id="@+id/v_line_end"
        android:layout_width="2dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:background="@color/color_f05a32" />

    <View
        android:id="@+id/v_line_top"
        android:layout_width="0dp"
        android:layout_height="2dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/color_f05a32" />

    <View
        android:id="@+id/v_line_bottom"
        android:layout_width="0dp"
        android:layout_height="2dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@color/color_f05a32" />

    <ImageView
        android:id="@+id/iv_delete"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_margin="20dp"
        android:src="@drawable/ic_delete_orange_30"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

活动

<?xml version="1.0" encoding="utf-8"?>

<data>

    <import type="android.view.View" />

    <variable
        name="positionOfGrid"
        type="Integer" />

    <variable
        name="isSelectView"
        type="Boolean" />

</data>


<LinearLayout
    android:id="@+id/llt_main"
    style="@style/lltMainComponent">

    <com.foodmarco.resapp.view.component.StoryComponentFrame
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:story_component_frame_is_show_frame="true">

        <EditText
            android:id="@+id/ed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="sasassasas\nsasa\nsa\nsaasas\nsasas"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </com.foodmarco.resapp.view.component.StoryComponentFrame>

</LinearLayout>

在Activity中,当编辑文本内容更新时,视图四个边框不更新高度。

enter image description here

【问题讨论】:

    标签: android kotlin view


    【解决方案1】:

    问题是您没有构建带有四个边框 Views(和图标)的 ConstraintLayout,您正在构建 ConstraintLayout,其中还有另一个 ConstraintLayout,其中有四个 Views

    inflate(最后一个参数集true)正在自动添加整个膨胀的View。您在扩展 ConstraintLayout(命名为 StoryComponentFrame)中调用此方法,所以基本上您将下一个新膨胀的 ConstraintLayout(所有孩子)添加到第一个

    熟悉HERE 中的&lt;merge&lt;include 标签,并将XML 中的根ConstraintLayout 交换为&lt;merge xmlns:android="http://schemas.android.com/apk/res/android"&gt; - 只有Views/childs 会被夸大并添加到StoryComponentFrame

    顺便说一句。为每个边缘添加一个 View 只是为了边框非常低效...考虑使用带有边框(笔划)的Drawable,例如HERE

    【讨论】:

    • 感谢您的帮助。我使用合并标签来解决我的问题。我使用四个视图作为边框,因为我需要边框可以覆盖另一个子视图。如果我使用drawable作为父视图的边框背景,它不能覆盖另一个视图。不过,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多