简体   繁体   中英

Android set measure height

I was having some problem when trying to set the height for the linear layout with expand/collapse animation. Here is my 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>

And when my button onClick, it will either execute expand() or 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();
}

But I'm getting this output:

在此处输入图片说明

The height of the News Feed is way too small.I wonder is there any ways to set it to fill parent?

Thanks in advance.

I figured out a way to solve this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM