即使在从GONE变为VISIBLE之前,也可以准确地确定视图的测量高度

Che*_*eng 11 android

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

  • 标题文本视图和价格文本视图,始终可见.
  • 说明文本视图,可以显示或消失,具体取决于展开或折叠.

折叠(在应用启动期间)

在此输入图像描述

扩展(当用户点击它时)

在此输入图像描述

我希望周围有一些不错的动画.所以,我提到了/sf/answers/936685991/

其中一个关键因素是,即使在可见之前,也要知道描述文本视图的确切高度.

但是,我意识到了一些类型的代码.他们不准确

// v is description text view.
v.measure(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
Run Code Online (Sandbox Code Playgroud)
// 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();
Run Code Online (Sandbox Code Playgroud)

这将返回值32.(正确的测量高度假设为92).这是第一行文本的高度.这最终我的动画结束了

在此输入图像描述

我是否知道,确定视图的测量高度的正确方法是什么,甚至在它从GONE变为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>
Run Code Online (Sandbox Code Playgroud)

nmw*_*nmw 34

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

v.measure(MeasureSpec.makeMeasureSpec(parentView.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(MAX_HEIGHT, MeasureSpec.AT_MOST));
final int targetHeight = v.getMeasuredHeight();
Run Code Online (Sandbox Code Playgroud)

您需要引用ViewGroup v作为获取其宽度的子项,并定义MAX_HEIGHT(或者使用父View的高度?).

此外,您应该更改水平LinearLayout内的两个TextView的高度参数wrap_content,因为match_parent这里使用可能会导致问题.LinearLayout设置为wrap_content,但两个子项不指定高度.