使用权重在LinearLayout上展开和折叠动画

时间:2015-01-13 06:12:55

标签: android android-layout animation android-linearlayout

您好我设计了如下的线性布局:

<Linear Layout 1 > -- > weightSum = 100
 <Linear Layout 2> -- > weight ->10
 <Linear Layout 3> -- > weight ->10
 <Linear Layout 4> -- > weight ->60
    <listview></listview>
 <Linear Layout 5 > -- > weight ->10
    <Linear Layout> ->visibility GONE
 <Linear Layout 6> -- > weight ->10
</Linear Layout > 

我使用了权重,因此布局高度是固定的,不可滚动。

现在我有一个<Linear Layout> whose visibility is GONE to VISIBLE onclick,在这种情况下,我可以减少ListView高度,以便正确扩展LinearLayout填充。

我使用了下面的动画,但它缩小了上面的视图或线性布局。

public static void expand(final View v) {
    v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    final int targetHeight = v.getMeasuredHeight();

    v.getLayoutParams().height = 0;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? LayoutParams.WRAP_CONTENT
                    : (int)(targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

1 个答案:

答案 0 :(得分:0)

试试这个

v.measure(0, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));