得到WRAP_CONTENT高度

时间:2016-12-07 10:05:17

标签: java android animation height android-linearlayout

我的目的是让一个看不见的LinearLayout在点击特定按钮时显示为动画。为此,我将默认高度设置为WRAP_CONTENT,获取应用程序启动时的高度,将高度设置为0,并在单击按钮时启动动画。这是代码:

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高度,并将其传递给:

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

我怎么能这样做?我搜索了但发现了什么。

1 个答案:

答案 0 :(得分:14)

试试这段代码:

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