Andorid ViewGroup Childs IS不介绍后大小

发布于 2025-01-27 10:55:13 字数 2197 浏览 4 评论 0原文

我想用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):

enter image description here

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何必那么矫情 2025-02-03 10:55:13

Override onMeasure这样的方式:

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec)
}

Override onMeasure like this way:

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文