Crop and display the child, allowing the child to take the entire height of its contents, setting the height as wrap_content?

My child LinearLayout has a dynamic height, it can change at runtime, it can contain images and consists of a different number of child views (which can also have children).

This LinearLayout child is inside a CardView. Now I want this type of card to be collapsible. In other words, when it crashes, it should have a lower height (e.g. 150dp). In this mode, the map should only display the top 150dp (minus fill) of the LinearLayout child content. But LinearLayout should have a maximum size (although the lower border below the CardView border is not shown, cropped), setting its height as wrap_content. This is similar to what would happen if I placed a ScrollView (when scrolling up) instead of CardView. But when I do it using CardView. LinearLayout does not take the maximum height to wrap the growth of its children, instead, it occupies the height limited by its parent card.

This results in lower image scaling and can lead to many unexpected errors.

Is there a way to achieve functionality? Is there a different type of layout, other than card cards, that directly supports kruping and displays only part of its contents, while the child feels that it can expand unlimitedly.

+6
source share
1 answer

Extend LinearLayoutwhich you use in CardViewwith another class (e.g. MyFrameLayout) and override the method:

@Override
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
    super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, View.MeasureSpec.UNSPECIFIED, heightUsed);
}

Then use it in the layout, and at runtime you can change the height parameter.

+3
source

All Articles