How to get ViewSwitcher to wrap only the currently

2019-04-04 16:45发布

问题:

I'm using a ViewSwitcher that lets me easily flip back and forth between two different List Headers with Animations built in.

However, the problem is the two views are of greatly varying height and it appears that the ViewSwitcher with layout_height="wrap_content", measures both children and takes the greater of the two heights as its own. This results in a large amount of unwanted blank space immediately below the smaller view when it is showing.

I understand why this is desired ViewSwitcher behavior, but it is NOT desired My App behavior.

Example:
Say View A is 50dp tall and View B is 100dp tall and both are added as children to the ViewSwitcher. The ViewSwitcher always is 100dp tall even with View A showing.

What I want is to be able to switch between A and B and have the ViewSwitcher shrink or grow to fit whichever view is currently displayed. How can I do this?

回答1:

Set the measureAllChildren attribute on the ViewSwitcher to false.

<ViewSwitcher
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:measureAllChildren="false">

For more info see documentation.