问题描述
当用户拖动某个视图时,我需要使用 ValueAnimator 使自定义放置字段"出现.(我想将字段从 gone, height = 0
更改为 visible, height = wrap_content
).
I need to use a ValueAnimator to make a custom "drop field" appear when the user drags a certain view. (I want to change the field from gone, height = 0
to visible, height = wrap_content
).
我已经尝试过这个问题的解决方案:如何为 wrap_content 设置动画?
I've tried the solution of this question: How to animate to wrap_content?
当我在单个 TextView 上使用它时,那里的答案有效,但是当我尝试将它应用于具有多个文本视图的 LinearLayout 时,它的动画高度值太大,然后,当动画完成时,又回到正确的一个.布局:
The answer there worked when I used it on a single TextView, but when I tried to apply it to a LinearLayout with multiple text views it animated to a too large height value, then, when the animation finished, snapped back to the correct one. The layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="gone"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="match_parent"
android:paddingVertical="8dp"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:paddingVertical="8dp"
android:gravity="center"
android:textSize="18sp"/>
</LinearLayout>
使用 animateLayoutChanges 对我也不起作用,无论如何我想使用自定义动画制作器.
Using animateLayoutChanges didn't work for me either, and I want to use a custom animator anyway.
我在 Xamarin 中使用 C#,但使用 Android Studio Java 代码回答也是可以接受的,我会将其转换为 C#.
I'm using C# in Xamarin, but answering with Android Studio Java code is acceptable too, I'll translate it to C#.
推荐答案
这应该是在Measure期间测量子视图引起的,因此您可以像这样更改您的xaml,将您的子视图的宽度更改为wrap_content代码>:
This should be caused by measuring the children views during Measure, so you can change your xaml like this ,change your children views's width to wrap_content
:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="gone"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_weight="5"
android:layout_height="match_parent"
android:paddingVertical="8dp"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_weight="2"
android:layout_height="match_parent"
android:paddingVertical="8dp"
android:gravity="center"
android:textSize="18sp"/>
</LinearLayout>
这篇关于Android:如何将高度设置为 wrap_content?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!