我有一个RelativeLayout,其中有一个ScrollView和一个EditText,如果用户将焦点转移到EditText,我希望ScrollView隐藏自己直到它们完成。这部分很简单,但我也希望相对布局逐渐收缩/扩展以呈现良好的过渡,而不是快速打开/关闭。
以下是当 EditText 获得焦点时隐藏 ScrollView 的代码:
私有(private)类 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_,但这并不能实现我想要的动画效果。我显然错过了这里的概念,并且非常感谢一些关于我如何错误地概念化这个概念的指示。
最佳答案
您是否尝试过内置 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);
}
}
});
}
}
测试.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>
关于android - 当其内容发生变化时,很难对relativelayout进行动画调整以改变大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27456066/