获取WRAP_CONTENT的高度 [英] Get what the WRAP_CONTENT height would be

查看:565
本文介绍了获取WRAP_CONTENT的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是让一个不可见的LinearLayout在单击特定按钮时以动画形式显示。为此,我已将默认高度设置为WRAP_CONTENT,在应用启动时获取高度,将高度设置为0,然后在我单击按钮时启动动画。这是代码:

My purpose is to have an invisible LinearLayout that appear whit an animation when click on a specific button. To do this i have set the default height to WRAP_CONTENT, get the height when the app start, set the height to 0 and start the animation when i click on the button. Here is the code:

linearLayout.post(new Runnable() {
    @Override
    public void run(){
        height = linearLayout.getMeasuredHeight();
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0));
    }
});


findViewById(R.id.btnOperator).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Animation ani = new ShowAnim(linearLayout, height/* target layout height */);
        ani.setDuration(1000/* animation time */);
        linearLayout.startAnimation(ani);

    }
});

这项工作相当不错,但是我想做些不同的事情。我希望默认高度为0,然后计算WRAP_CONTENT的高度,然后将其传递给:

This work pretty good, but i want to do different. I want that the default height is 0, and then calculate what the WRAP_CONTENT height would be, and pass it to:

Animation ani = new ShowAnim(linearLayout, height/* target layout height */);

我该怎么做?我搜索了但是发现了什么。

How could i do this? I searched but found anything.

推荐答案

尝试以下代码:

linearLayout.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
height = linearLayout.getMeasuredHeight();

这篇关于获取WRAP_CONTENT的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆