Android设置测量高度

时间:2014-12-03 13:44:29

标签: java android measure

尝试使用展开/折叠动画设置线性布局的高度时遇到了一些问题。这是我的XML:

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/btnNewsFeed"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/lightred"
            android:minHeight="20dp"
            android:paddingBottom="5dp"
            android:text="News feed"
            android:textColor="#FFFFFF"
            android:textSize="13dp" />

        <LinearLayout
            android:id="@+id/llNewsFeed"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/btnNewsFeed"
            android:background="#FFFFFF"
            android:orientation="vertical"
            android:visibility="gone" >

            <ListView
                android:id="@+id/EventNewsFeedListview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:listSelector="#F6CECE" >
            </ListView>
        </LinearLayout>
    </RelativeLayout>

当我的按钮onClick时,它将执行expand()或colapse():

btnNewsFeed.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (llNewsFeed.getVisibility() == View.GONE) {
                expand();
            } else {
                collapse();
            }
        });

private void expand() {
    llNewsFeed.setVisibility(View.VISIBLE);
    final int widthSpec = View.MeasureSpec.makeMeasureSpec(0,
            View.MeasureSpec.UNSPECIFIED);
    final int heightSpec = View.MeasureSpec.makeMeasureSpec(0,
            View.MeasureSpec.UNSPECIFIED);
    llNewsFeed.measure(widthSpec, heightSpec);

    ValueAnimator mAnimator = slideAnimator(0,
            llNewsFeed.getMeasuredHeight());
    mAnimator.start();
}

private ValueAnimator slideAnimator(int start, int end) {
    ValueAnimator animator = ValueAnimator.ofInt(start, end);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int value = (Integer) valueAnimator.getAnimatedValue();
            ViewGroup.LayoutParams layoutParams = llNewsFeed
                    .getLayoutParams();
            layoutParams.height = value;
            llNewsFeed.setLayoutParams(layoutParams);
        }
    });
    return animator;
}

private void collapse() {
    int finalHeight = llNewsFeed.getHeight();
    ValueAnimator mAnimator = slideAnimator(finalHeight, 0);
    mAnimator.addListener(new Animator.AnimatorListener() {
        public void onAnimationEnd(Animator animator) {
            llNewsFeed.setVisibility(View.GONE);
        }

        public void onAnimationCancel(Animator arg0) {
        }

        public void onAnimationRepeat(Animator arg0) {
        }

        public void onAnimationStart(Animator arg0) {
        }
    });
    mAnimator.start();
}

但是我得到了这个输出:

enter image description here

新闻Feed的高度太小了。我想知道有没有办法设置它来填充父母?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法:

ValueAnimator mAnimator = slideAnimator(0,
        llNewsFeed.getMeasuredHeight() * 3);