ViewSwitcher和Android的布局高度布局、高度、ViewSwitcher、Android

2023-09-05 02:33:48 作者:余生一个薛之谦

我添加了一个ViewSwitcher到的LinearLayout,其中有两种观点不同的高度,但似乎ViewSwitcher是占据最大的意见的空间,而不是安排本身。这是应该的?

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 arranging 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

推荐答案

ViewSwitcher的默认行为,继承ViewAnimator,是考虑在布局中的所有子视图,这将导致ViewSwitcher占据最大的子空间

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.

要改变这一点,所有你需要做的是设置MeasureAllChildren标志设置为false。这将使布局传递忽略当前隐藏在子视图。例如设置该标志在活动的onCreate方法,例如:

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例子:

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>