Android 当ConstraintLayout 高度变化时,自定义ConstraintLayout 无法更新子高度
Android custom ConstraintLayout cannot update child height when ConstraintLayout height change
我已经创建了一个自定义视图,它正在扩展 ConstraintLayout,然后添加四个视图作为边框。
当我在 Activity xml 中更新子项高度时,尽管我已经将视图约束顶部、开始、结束和底部设置为父级,但四个视图无法更新高度。
但是,如果我将四个视图设置为 activity 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>
Activity
<?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
问题是您没有构建具有四个边框 View
(和图标)的 [=10=],您正在构建 ConstraintLayout
,它还有另一个 ConstraintLayout
,里面有四个 View
s
inflate
(最后一个参数设置 true
)正在自动添加整个膨胀 View
。您在扩展 ConstraintLayout
(名为 StoryComponentFrame
)中调用此方法,所以基本上您是将下一个新膨胀的 ConstraintLayout
(包括所有孩子)添加到第一个
熟悉 HERE 中的 <merge
和 <include
标签,并将 XML 中的根 ConstraintLayout
交换为 <merge xmlns:android="http://schemas.android.com/apk/res/android">
- 仅 View
s/childs 将被膨胀并添加到 StoryComponentFrame
顺便说一句。为边框的每个边缘添加一个 View
是非常低效的......考虑使用 Drawable
和边框(笔划),如 HERE
我已经创建了一个自定义视图,它正在扩展 ConstraintLayout,然后添加四个视图作为边框。 当我在 Activity xml 中更新子项高度时,尽管我已经将视图约束顶部、开始、结束和底部设置为父级,但四个视图无法更新高度。
但是,如果我将四个视图设置为 activity 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>
Activity
<?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
问题是您没有构建具有四个边框 View
(和图标)的 [=10=],您正在构建 ConstraintLayout
,它还有另一个 ConstraintLayout
,里面有四个 View
s
inflate
(最后一个参数设置 true
)正在自动添加整个膨胀 View
。您在扩展 ConstraintLayout
(名为 StoryComponentFrame
)中调用此方法,所以基本上您是将下一个新膨胀的 ConstraintLayout
(包括所有孩子)添加到第一个
熟悉 HERE 中的 <merge
和 <include
标签,并将 XML 中的根 ConstraintLayout
交换为 <merge xmlns:android="http://schemas.android.com/apk/res/android">
- 仅 View
s/childs 将被膨胀并添加到 StoryComponentFrame
顺便说一句。为边框的每个边缘添加一个 View
是非常低效的......考虑使用 Drawable
和边框(笔划),如 HERE