确定视图高度的准确方法,甚至在视图从“消失"更改为“可见"之前 [英] Accurate way to determine the measured height of a view, even before it changed from GONE to VISIBLE

查看:61
本文介绍了确定视图高度的准确方法,甚至在视图从“消失"更改为“可见"之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个看起来像这样的布局.它包含.

Currently, I have a layout which looks like this. It contains.

  • 标题文本视图和价格"文本视图,该视图始终可见.
  • 描述文本视图,根据扩展或折叠,它可以显示或消失.
  • Title text view, and Price text view, which is visible always.
  • Description Text View, which can be visible or gone, depending expanding or collapsing.

我希望周围有一些不错的动画.因此,我提到了 https://stackoverflow.com/a/13381228/72437

I want to have some nice animation around it. So, I referred to https://stackoverflow.com/a/13381228/72437

关键要素之一是甚至在可见之前都知道 Description Text View 的确切高度.

One of the key element, is to know the exact height of Description Text View, even before it is visible.

但是,我实现了几种类型的代码.他们不准确

However, I realize a few type of code. They are't accurate

// v is description text view.
v.measure(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();


// v is description text view.
v.measure(MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY));
final int targtetHeight = v.getMeasuredHeight();

这将返回值32.(正确的测量高度应该是92).这是文本第一行的高度.这样我的动画就结束了

This will return value 32. (The correct measured height suppose to be 92). This is the height for first line of text. This ends up my animation is ended at

我可以知道,即使在视图从消失"更改为可见"之前,确定视图高度的正确方法是什么?

May I know, what is the correct way to determine the measured height of a view, even before it changed from GONE to VISIBLE?

我的布局代码如下:

        <LinearLayout
            android:clickable="true"
            android:id="@+id/chart_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/dummy"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="0dp"
                    android:width="0dp"
                    android:layout_weight="0.6"
                    android:layout_height="match_parent"
                    android:gravity="left"
                    android:textSize="20sp" 
                    android:textColor="#ff000000"
                    android:text="Summary chart" />
                <TextView
                    android:id="@+id/chart_price_text_view"
                    android:layout_width="0dp"
                    android:width="0dp"
                    android:layout_weight="0.4"
                    android:layout_height="match_parent"
                    android:gravity="right"
                    android:textSize="20sp" 
                    android:textColor="#ffF76D3C"
                    android:text="$2.99" />

            </LinearLayout>
            <TextView
                android:visibility="gone"
                android:id="@+id/chart_description_text_view"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"         
                android:text="@string/currency_exchange_description"
                android:textColor="#ff626262"
                android:textSize="15sp" />                 
        </LinearLayout>

推荐答案

您的第二个代码段几乎是正确的,但是您需要指定像素大小-而不是 FILL_PARENT/MATCH_PARENT .这应该起作用:

Your 2nd code snippet is almost correct, but you need to specify pixel sizes - not FILL_PARENT/MATCH_PARENT. This should work:

v.measure(MeasureSpec.makeMeasureSpec(parentView.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(MAX_HEIGHT, MeasureSpec.AT_MOST));
final int targetHeight = v.getMeasuredHeight();

您需要引用一个视图组,该视图组是 v 的子级,以获取其宽度,并定义 MAX_HEIGHT (或者使用父视图的高度?).

You'll need to have a reference to the ViewGroup that v is a child of to get its width, and define MAX_HEIGHT (or perhaps use the parent View's height?).

此外,您应该将水平LinearLayout内的两个TextView的高度参数更改为 wrap_content ,因为在此处使用 match_parent 可能会引起问题.LinearLayout设置为wrap_content,但是两个子级未指定高度.

Also, you should change the height parameters of the two TextViews that are within the horizontal LinearLayout to wrap_content, as using match_parent here may cause problems. The LinearLayout is set to wrap_content, but the two children don't specify a height.

这篇关于确定视图高度的准确方法,甚至在视图从“消失"更改为“可见"之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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