一般情况:
View's getWidth() and getHeight() returns 0
View初始状态为GONE的情况
private int mMeasuredHeight;
private int mMeasuredWidth;
private boolean firstVisible = true;
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == View.VISIBLE) {//可见状态
if (firstVisible) {
mSeekBar.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mMeasuredWidth = mSeekBar.getMeasuredWidth();
mMeasuredHeight = mSeekBar.getMeasuredHeight();
Log.e("", "onGlobalLayout " + mMeasuredWidth + " " +
mMeasuredHeight);
mSeekBar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
firstVisible = false;
}
}
}