android - 当ConstraintLayout高度改变时Android自定义ConstraintLayout不能更新子高度
问题描述
我创建了一个自定义视图正在扩展 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中,当编辑文本内容更新时,视图的四个边框没有更新高度。
解决方案
问题是你没有ConstraintLayout
用四个边框View
(和图标)构建,你正在构建,ConstraintLayout
它还有另一个ConstraintLayout
,里面有四个View
inflate
(带有最后一个参数集true
)正在自动添加整个充气View
。您在扩展ConstraintLayout
(命名StoryComponentFrame
)中调用此方法,因此基本上您将下一个新膨胀ConstraintLayout
(与所有孩子)添加到第一个
熟悉HERE<merge
中的<include
标签并将您的根在 XML 中交换为- 只有s/childs 会被夸大并添加到ConstraintLayout
<merge xmlns:android="http://schemas.android.com/apk/res/android">
View
StoryComponentFrame
顺便提一句。View
只为边框添加一个边缘是非常低效的......考虑使用Drawable
边框(笔划),就像在这里
推荐阅读
- amazon-web-services - 成云模板中如何根据阶段设置环境变量
- php - 如何解决致命错误:未捕获的错误?
- android - 为什么 Android Studio 在 View -> Tool Windows 中缺少“调色板”?
- angular - 角度服务方法不是功能产品构建
- reactjs - react-facebook-login 与生命周期问题
- python - Why is my program running an infinite loop? Python
- javascript - 数组内不可变的 redux 对象更新
- html - Achor 标签在刷新后禁用和启用
- html - CSS 网格溢出
- python - 有没有办法通过列名访问 SQL 行数据?