Andorid ViewGroup Childs IS不介绍后大小
我想用imageView
和其他一些视图创建自己的ViewGroup。我这样做如下(我已经删除了简单地添加其他视图以简单地示例):
class TestWidget @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
init {
addViews()
}
private fun addViews() {
val backgroundView = ImageView(context)
backgroundView.setImageDrawable(resources.getDrawable(R.drawable.kitten)) // image is 640x640 px
addView(backgroundView)
val text = TextView(context)
text.text = "My awesome cat"
text.textSize = 10.dp.toFloat()
text.setTextColor(Color.WHITE)
addView(text)
text.layoutParams = LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
).apply {
rightToRight = id
leftToLeft = id
topToTop = id
bottomToBottom = id
}
}
}
我将此小部件放入XML中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<com.test.TestWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
结果是完美的(中心的猫):
i.sstatic.net/xoan7.png“ rel =“ nofollow noreferrer” > 我想让我的testwidget总是平方的。为此,我被覆盖onMeasure
如下(用于肖像布局):
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
setMeasuredDimension(measuredWidth, measuredWidth)
}
之后我的猫被打破了:
如您所见,我的窗口高度等于其宽度,但是imageView
没有重新绘制,并且被切断。 TextView
也没有重新绘制,也不位于我的小部件的中心。我想删除ImageView
上方的空白空间。
我尝试通过不同的方式来修复此行为:将布局参数设置为“使用约束”,call invalidate()
and request> request> requestlayout()来自onsizezechanged()
回调,但没有任何帮助。我做错了什么?
I want to create my own ViewGroup with ImageView
and some other views. I do this as follows (i have removed adding of other views for simplyfing the example):
class TestWidget @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
init {
addViews()
}
private fun addViews() {
val backgroundView = ImageView(context)
backgroundView.setImageDrawable(resources.getDrawable(R.drawable.kitten)) // image is 640x640 px
addView(backgroundView)
val text = TextView(context)
text.text = "My awesome cat"
text.textSize = 10.dp.toFloat()
text.setTextColor(Color.WHITE)
addView(text)
text.layoutParams = LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
).apply {
rightToRight = id
leftToLeft = id
topToTop = id
bottomToBottom = id
}
}
}
I put this widget into xml as follows:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<com.test.TestWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
The result is perfect (cat with text in the center):
Now i want to make my TestWidget always square. For that i am override onMeasure
as follows (for portrait layout):
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
setMeasuredDimension(measuredWidth, measuredWidth)
}
And after that my cat becomes broken:
As you can see, my widget's height becomes equal to its width, but the ImageView
is not redrawn, and is cut off. TextView
is not redrawn too and is situated not in the center of my widget. I want to remove that empty space above the ImageView
.
I tried to fix this behaviour with different ways: setting layout params to backgroundView with constraint, call invalidate()
and requestLayout()
from onSizeChanged()
callback, but nothing helps. What i am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Override
onMeasure
这样的方式:Override
onMeasure
like this way: