material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.37k stars 3.07k forks source link

[BottomNavigationView] return incorrect view height after measured #2296

Open Sanlorng opened 3 years ago

Sanlorng commented 3 years ago

Description: BottomNavigationView will return incorrect view height after measured

Expected behavior: BottomNavigationView return the correct view height afater measured

Source code: bottomNavigationView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED) view.updatePadding(bottom=bottomNavigationView.measuredHeight)

Android API version: Android API 30

Material Library version: 1.5.0-alpha01

Device: OnePlus 7 Pro

drchen commented 3 years ago

Can you provide more details?

Like, is it returning 0, or larger/smaller than the actual value?

Sanlorng commented 3 years ago

Can you provide more details?

Like, is it returning 0, or larger/smaller than the actual value?

the return value is 0 and BottomNavigationView can't preview in the Android Studio layout editor

illuzor commented 2 years ago

The same in 1.6.0, but no problem with 1.4.0 It happens when measure() called with MeasureSpec.UNSPECIFIED:

 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        bottomNavigation.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED)
        // ...
}

AT_MOST can be used as workaround :

bottomNavigation.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(heightMeasureSpec, MeasureSpec.AT_MOST))
hunterstich commented 2 years ago

Hi @Sanlorng,

When are you trying to get the height of the view? Are you able to wait until the view has been laid out until trying to get it's height?

hunterstich commented 2 years ago

@Sanlorng any more info on this? Is this still an issue?

Sanlorng commented 2 years ago

@Sanlorng any more info on this? Is this still an issue?

I need confirm again,I make a workaround code,which work fine at these months.

drchen commented 2 years ago

I'll close this issue for now due to no further info available. Please feel free to reopen it if you confirm this is still happening.

mateuszkwiecinski commented 2 years ago

@drchen 👋 I think I faced the same issue, when trying to upgrade from 1.4.0 to the latest stable (1.6.1 at this point)

This change: https://github.com/material-components/material-components-android/commit/7eea2b25bc47553c9b11b7ec5268257588601c76 changed the behavior how onMeasure works.

When trying to manually measure the view with makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), the requested measure spec gets overridden with 0, MeasureSpec.EXACTLY which then results in unwanted behavior ("Unwanted" meaning the measured height is 0). The expected behavior is to respect the minHeight attribute.

Answering one of the previous questions: my use case is to manually measure the view to then capture it with https://github.com/facebook/screenshot-tests-for-android and store as a screenshot for later comparison.

When are you trying to get the height of the view? Are you able to wait until the view has been laid out until trying to get it's height?

I call measure(x, makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)) and then immediately layout(0, 0, measuredWidth, measuredHeight)