我只想在xml中设置
时间: 2024-02-27 15:53:21 浏览: 19
如果你只想在 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` 在隐藏时添加一个向上平移并渐隐的动画效果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)