[英]Translate animation has no effect
我希望视图从布局的右下角移至中心。 我在RelativeLayout内只有一个textView,并编写了一个简单的代码对其进行测试,但没有用。
这是我的动画XML;
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="50%p"
android:toYDelta="50%p"
android:duration="1000"
android:fillAfter="true" />
这是我的布局文件;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="x" />
</RelativeLayout>
这是活动的相关部分;
x = (TextView) findViewById(R.id.textView1);
Animation translate = AnimationUtils.loadAnimation(MainActivity.this,
R.anim.viewanimation);
x.startAnimation(translate);
问题是,textView“ x”不会移动。 我已经读过,在XML文件中指定%p
会使它相对于父级。 由于我希望视图从其原始位置移动到屏幕中心,因此将“ from”和“ to”值设置为0%
和50%p
。 我观察到,当我删除%p
属性并将其从0%
移到-100%
,它可以正确移动。 因此,相对定位在这里似乎不起作用。 我可能做错了什么?
问题解决了。 相对值应为负,如下所示;
android:toXDelta="-50%p"
android:toYDelta="-50%p"
因为正值会将视图移出布局。 因此, %0p
值对应于布局的右下角。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.