I use the following technique - post a runnable from onCreate()
that will be executed when the view has been created:
contentView = findViewById(android.R.id.content);
contentView.post(new Runnable()
{
public void run()
{
contentHeight = contentView.getHeight();
}
});
This code will run on the main UI thread, after onCreate()
has finished.
If you are trying to get the height of the content view itself, you need to subtract the padding from the height - this is because Android 2.x lays out the views differently to 4.x.
contentHeight = contentView.getHeight() - contentView.getTopPadding();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…