【发布时间】:2020-06-24 10:42:11
【问题描述】:
在尝试将视图从消失变为可见时,我遇到了一些奇怪的影响。见附件 gif。
我有一个简单的Switch,它可以切换TextView 的可见性。当TextView 变为可见时,Button 上会出现不希望的动画效果。
我认为这与R.id.group 布局高度有关。请参阅下面的布局。
当我删除ScrollView 并将R.id.group 布局高度更改为match_parent 时,它可以正常工作。但这不是解决方案,因为我最终需要ScrollView。
如何避免按钮动画效果?
源代码:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Switch sw = view.findViewById(R.id.toggle);
TextView tv = view.findViewById(R.id.text);
sw.setOnCheckedChangeListener((button, isChecked) -> {
int visibility = isChecked ? View.VISIBLE : View.GONE;
tv.setVisibility(visibility);
});
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="10dp"
>
<LinearLayout
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
>
<Switch
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="toggle"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Blabla\nblabla\nblabla\nblabla"
/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Self destruct"
/>
</LinearLayout>
</ScrollView>
【问题讨论】:
标签: android android-layout animation button android-scrollview