[Android / Kotlin]: When are views initialized in

2019-08-08 13:54发布

问题:

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.

回答1:

You can try using viewTreeObserver.

val vto = button.viewTreeObserver
vto.addOnGlobalLayoutListener {
    Log.e("Show me width", button.width.toString())
}

It is working, but it can and WILL be called several times!!!

Other option is to use Handler and postDelayed

 Handler().postDelayed({
        Log.e("Show me width2", button.width.toString())
    }, 1000)

Yes, this is very bad practice, but it can save you in stupid situation :)

Good luck!



回答2:

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

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.

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)

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)