我有一个RelativeLayout,其中是ScrollView和EditText,如果用户将焦点转移到EditText,我希望ScrollView隐藏自己直到完成。这部分很简单,但我也希望RelativeLayout逐渐缩小/扩展以呈现一个很好的过渡,而不是捕捉开/关。
以下是在EditText获得焦点时隐藏ScrollView的代码:
私有类myCostBoxFocusListener实现了View.OnFocusChangeListener { public void onFocusChange(View v, boolean hasFocus) {
ScrollView sView = (ScrollView) ((RelativeLayout)v.getParent().getParent()).getChildAt(1);
//A bit hacky, I know, but it works.
if (hasFocus) {
sView.setVisibility(View.GONE);
}
else if(!hasFocus) {
sView.setVisibility(View.VISIBLE);
}
}
}
这很好用。但是,我试图设置打开/关闭动画的代码并不起作用。它当前导致ScrollView项目在2秒内淡入/淡出,而RelativeLayout仍然立即开启/关闭。
LayoutTransition transition = new LayoutTransition();
ObjectAnimator a1 = ObjectAnimator.ofFloat(null, View.SCALE_Y, 0, 1);
AnimatorSet animator = new AnimatorSet();
animator.setStartDelay(0);
animator.play(a1);
transition.setAnimator(LayoutTransition.CHANGE_APPEARING, animator);
transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, animator);
transition.setDuration(2000);
RelativeLayout rView = (RelativeLayout) dialogNew.findViewById(R.id.ocrmain_group_select_parent);
rView.setLayoutTransition(transition);
我尝试过使用LayoutTransition_APPEARING / DISAPPEARING代替CHANGE_,但这并没有动画我想要的动画。我在这里显然错过了这个概念,并且非常感谢我对如何错误地概念化这个概念。
答案 0 :(得分:3)
您是否尝试过内置的layout change animation?
android:animateLayoutChanges="true"
添加到您的相对布局应该做你想要的。
编辑:要更改默认动画的持续时间,您可以执行以下操作。
public class MainActivity extends Activity {
private ScrollView scrollView;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.test);
super.onCreate(savedInstanceState);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
LayoutTransition t = relativeLayout.getLayoutTransition();
t.setDuration(2000);
relativeLayout.setLayoutTransition(t);
scrollView = (ScrollView) findViewById(R.id.scroll_view);
EditText editText = (EditText) findViewById(R.id.edit_text);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
scrollView.setVisibility(View.GONE);
}
else {
scrollView.setVisibility(View.VISIBLE);
}
}
});
}
}
的test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:padding="5dp" >
<LinearLayout android:id="@+id/linear_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true" android:focusableInTouchMode="true"
android:layout_alignParentTop="true" >
<EditText android:id="@+id/edit_text"
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:selectAllOnFocus="true"
android:imeOptions="actionDone"
android:inputType="numberDecimal" />
<EditText android:id="@+id/edit_text2"
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:selectAllOnFocus="true"
android:imeOptions="actionDone"
android:inputType="numberDecimal" />
</LinearLayout>
<ScrollView android:id="@+id/scroll_view"
android:paddingTop="20dp"
android:gravity="end"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:layout_below="@id/linear_layout" >
<TableLayout android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView android:layout_height="30dp" android:layout_width="match_parent" android:text="TTTT"/>
<TextView android:layout_height="30dp" android:layout_width="match_parent" android:text="TTTT"/>
<TextView android:layout_height="30dp" android:layout_width="match_parent" android:text="TTTT"/>
<TextView android:layout_height="30dp" android:layout_width="match_parent" android:text="TTTT"/>
<TextView android:layout_height="30dp" android:layout_width="match_parent" android:text="TTTT"/>
<TextView android:layout_height="30dp" android:layout_width="match_parent" android:text="TTTT"/>
<TextView android:layout_height="30dp" android:layout_width="match_parent" android:text="TTTT"/>
</TableLayout>
</ScrollView>
<Button android:id="@+id/button_cancel"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_below="@id/scroll_view"
android:layout_alignParentLeft="true" />
<Button android:id="@+id/button_ok"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Ok"
android:layout_below="@id/scroll_view"
android:layout_alignParentRight="true" />
</RelativeLayout>