设置setVisibility没效果

16 篇文章 0 订阅

省略背景,直接入主题
我在设置view的可见性时候,一直没反应
view.setVisibility(View.INVISIBLE);
原来是因为我对这个view设置了动画。
并且将动画加了了一个设置属性:  anim.setFillAfter(true);
这个会导致上面那个bug。所以应该要来个


方案一:
 anim.setFillAfter(false);
view.setVisibility(View.INVISIBLE);
或者方案二:
ivCircle.clearAnimation();
ivCircle.setVisibility(View.INVISIBLE);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果你只想在 XML 中设置隐藏 View 的动画,可以使用 View 的 `android:animateLayoutChanges` 属性。这个属性可以让 ViewGroup 在添加、移除或隐藏 View 时自动添加动画效果。 首先,在你的布局文件中,设置需要隐藏的 View 的初始状态为可见: ```xml <TextView android:id="@+id/my_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:visibility="visible" /> ``` 接下来,将 `android:animateLayoutChanges` 属性设置为 `true`: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true"> <!-- 在此添加其他 View --> <TextView android:id="@+id/my_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:visibility="visible" /> <!-- 在此添加其他 View --> </LinearLayout> ``` 现在,当你将 `my_text_view` 的可见性设置为 `View.GONE`,它将会自动添加默认的淡出动画效果: ```java TextView textView = findViewById(R.id.my_text_view); textView.setVisibility(View.GONE); ``` 你也可以为 View 添加自定义的动画效果,比如平移或者缩放。只需要在 `android:animateLayoutChanges` 属性为 `true` 的情况下,使用 View 的 `animate()` 方法添加动画即可: ```java TextView textView = findViewById(R.id.my_text_view); textView.animate() .translationY(-textView.getHeight()) .alpha(0) .setDuration(500) .withEndAction(new Runnable() { @Override public void run() { textView.setVisibility(View.GONE); } }); ``` 这段代码将会使 `my_text_view` 在隐藏时添加一个向上平移并渐隐的动画效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值