android - 具有预定义视图的自定义视图组无法正常工作
问题描述
我正在使用以下代码:
class TestConstraintLayout(context: Context): ConstraintLayout(context) {
private val simpleConstraintSet = SimpleConstraintSet(this)
private val textView = AppCompatTextView(context)
init {
textView.id = View.generateViewId()
textView.layoutParams = ConstraintLayout.LayoutParams(
10,
ConstraintLayout.LayoutParams.MATCH_PARENT
)
textView.setBackgroundColor(-10000)
simpleConstraintSet.build {
textView leftToLeftOf parent
textView topToTopOf parent
textView rightToRightOf parent
textView bottomToBottomOf parent
textView withHorizontalBiasOf 0.3f
}
addView(textView)
}
}
尽管存在水平偏差,但我得到以下结果:
一切都在我的自定义布局之外完美运行,所以我想这不是设置 layoutparams 还是其中的约束的方法。
解决方案
我发现了我的错误,我应该在约束和参数之前添加带有 addView 的 textView。
推荐阅读
- c++ - fmt std::string 显示为数字
- java - 在 websphere 分布式事务中使用 teradata 数据源
- php - Slim Framework 4 - 不在对象中时使用 $this (Medoo Framework) PHP
- arrays - 数据列表中的动态数组未显示
- java - 如何在 SSLContextBuilder.create().loadTrustMaterial 的 JUnit 测试中引发异常?
- php - 如何只允许数组中的数据进入输入字段?
- powershell - Powershell IE自动化如何传递标志来导航方法?
- macos - 如何在 Quartz 事件中读取鼠标增量?
- javascript - 显示 JSON 对象
- youtube-dl - Youtube-dl @只想将播放列表(文件名列表)下载到文本文件