开发者

ViewSwitcher and layout height in Android

I added a ViewSwitcher to a LinearLayout, which has two views of different height, However it seems the ViewSwitcher is occupying space of the biggest of the views, rather than arr开发者_开发知识库anging itself. Is this how it should be?

What to do in the other case? I was trying to create an accordion, where the title panel, when clicked grows in size


The default behavior of ViewSwitcher, as inherited by ViewAnimator, is to consider all child views on layout, which will result in the ViewSwitcher occupying the space of the largest child.

To change this, all you need to do is to set the MeasureAllChildren flag to false. This will make the layout pass ignore the child view that is currently hidden. Set this flag e.g. in the onCreate method of the activity, e.g.:

    ViewSwitcher switcher = (ViewSwitcher)findViewById(R.id.ViewSwitcher);
    switcher.setMeasureAllChildren(false);

XML example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜