简体   繁体   English

获得WRAP_CONTENT高度

[英]Get what the WRAP_CONTENT height would be

My purpose is to have an invisible LinearLayout that appear whit an animation when click on a specific button. 我的目的是让一个看不见的LinearLayout在点击特定按钮时显示为动画。 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. 为此,我将默认高度设置为WRAP_CONTENT,获取应用程序启动时的高度,将高度设置为0,并在单击按钮时启动动画。 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);

    }
});

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: 我希望默认高度为0,然后计算WRAP_CONTENT高度,并将其传递给:

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

How could i do this? 我怎么能这样做? I searched but found anything. 我搜索但发现了什么。

Try this code: 试试这段代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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