[Android/Kotlin]:生命周期何时初始化视图? [英] [Android / Kotlin]: When are views initialized in lifecycle?

查看:186
本文介绍了[Android/Kotlin]:生命周期何时初始化视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道一个Button(或任何其他视图)的大小. 但是生命周期中的所有过程(onCreate,onStart,OnResume)似乎都不知道,因为Button似乎尚未初始化!

I need to know the size of a Button (or any other view). But none of the procedures in lifecycle (onCreate, onStart, OnResume) seem to know it, as the Button seems not yet to be initialized!

...
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private var servOffset: Int=0  // Value depends on Layout/Orientation and Device

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btPunkte.setOnClickListener { doPunkt(true) }
        servOffset = btPR1.width/2  // 'btPR1' is a Button in 'Layout activity_main.*'
        //ToDo: Doesn't work! = remains 0 
    }

    override fun onResume() {
        super.onResume()
        // ToDo: unsolved! When do I get the size?? 
        //     onStart (or onResume) are invoked correctly, but don't know the value!
        //     ?? Button doesn't yet exist in Livecycle?!
        servOffset = btPR1.width/2  // //ToDo: Still doesn't work!  
        anzeigeAktualisieren()
    }

    private fun anzeigeAktualisieren() {
        // If ... move Button
        btPR1.x += servOffset //ToDo: unsolved offset remains 0 ?!
    }

    private fun doPunkt(links:Boolean) {
        anzeigeAktualisieren()
        ...
    }
...
}

我确实找到了何时绘制视图" ,以及其他几个线程,但它们并不能帮助我解决问题.

I did find "When are views drawn", and several other threads, but they didn't help me solve my problem.

推荐答案

我想我终于找到了SO上的文章,该文章提供了您正在寻找的背景知识:看一下

I think I finally found the post on SO which provides the background knowledge you're looking for: take a look at View's getWidth() and getHeight() returns 0

由于无法在onResume()中获得实际"宽度,因为此时运行时尚未确定该值,因此您可以在ViewTreeObserver的帮助下设置Button的初始位置>.

As it seems that it's impossible to get the "real" width in onResume() since the runtime has not yet determined the value at this time, you can set the initial position for your Button with the help of ViewTreeObserver.

onCreate()的以下几行显示了如何使用OnPreDrawListener(只是为了介绍另一个选项):

The following lines from onCreate() show how to use a OnPreDrawListener (just to introduce another option):

btPunkte = findViewById(R.id.btnPoints)
btPunkte.setOnClickListener { doPunkt(true) }
btPR1 = findViewById(R.id.btnPR1)
val vto = btPR1.viewTreeObserver
val listener: ViewTreeObserver.OnPreDrawListener = object : ViewTreeObserver.OnPreDrawListener {

    var doUpdate: Boolean = true

    override fun onPreDraw(): Boolean {
        if(doUpdate) {
            doUpdate = false
            btPR1.viewTreeObserver.removeOnPreDrawListener(this)
            val width: Int = btPR1.measuredWidth
            servOffset = width / 2
            anzeigeAktualisieren()
        }
        return true
    }
}
vto.addOnPreDrawListener (listener)

请注意,除了删除侦听器外,我还使用Boolean确保 btPR1 的位置仅被修改一次(通常会更频繁地调用onPreDraw(),确切的数字似乎是取决于周围的ViewGroup)

Note that besides removing the listener I also use a Boolean to make sure the position of btPR1 is only modified once (onPreDraw() will normally be called more often, the exact number seems to depend on the complexity of the surrounding ViewGroup)

这篇关于[Android/Kotlin]:生命周期何时初始化视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆