【问题标题】:Android: how to animate height to wrap_content?Android:如何将高度设置为 wrap_content?
【发布时间】:2020-05-05 19:33:29
【问题描述】:

当用户拖动某个视图时,我需要使用 ValueAnimator 来制作自定义的“放置字段”。 (我想将字段从gone, height = 0 更改为visible, height = wrap_content)。

我已经尝试过这个问题的解决方案:How to animate to wrap_content?

当我在单个 TextView 上使用它时,那里的答案有效,但是当我尝试将它应用到具有多个文本视图的 LinearLayout 时,它动画到一个太大的高度值,然后,当动画完成时,又回到了正确的一个。布局:

<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 对我也不起作用,我还是想使用自定义动画器。

我在 Xamarin 中使用 C#,但也可以使用 Android Studio Java 代码回答,我会将其翻译成 C#。

【问题讨论】:

  • 在 xml 中保持高度 wrap_content,然后以编程方式从 0 to initial measured height 开始 valueAnimator

标签: java c# android xamarin


【解决方案1】:

这应该是在Measure期间测量子视图引起的,因此您可以像这样更改您的xaml,将您的子视图的宽度更改为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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多