【发布时间】: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中,当编辑文本内容更新时,视图四个边框不更新高度。
【问题讨论】: