【问题标题】:Animation doesn't work when attached to the parent (RelativeLayout)layout?附加到父(RelativeLayout)布局时动画不起作用?
【发布时间】:2017-03-23 20:55:30
【问题描述】:

我尝试将按钮从顶部动画到屏幕上的固定位置,当在按钮上添加 clicklistener 时它可以工作,但在附加到 relativelayout 时它不会工作。这样包括 relativelayout 在内的按钮就会被动画化。我认为问题在于在嵌套视图组中附加布局时,请帮助我解决问题。提前致谢。

 b = (Button) findViewById(R.id.simpleButton);
    relativeLayout = (RelativeLayout) findViewById(R.id.wrapper);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            b.startAnimation(animations());
        }
    });
    relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            relativeLayout.setAnimation(animations());
        }
    });
}


private TranslateAnimation animations() {
    TranslateAnimation translateAnimation = new TranslateAnimation(0f, 0f, 0, -40);
    translateAnimation.setDuration(1000);
    translateAnimation.setInterpolator(new AccelerateInterpolator());
    return translateAnimation;
}

这里是 XML;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
     <RelativeLayout
android:id="@+id/wrapper"
android:layout_width="match_parent"
android:background="#cbcbbc"
android:layout_height="60dp">
    <Button
        android:id="@+id/simpleButton"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="Go next page" />
   </RelativeLayout>

   </LinearLayout>

【问题讨论】:

  • 发布xml布局。
  • 你想达到什么目标,会发生什么?
  • 当我按下按钮时,布局从顶部进入并进入其位置,同样我想通过包括父布局在内的整个布局来实现这一点。它仅适用于按钮点击,不适用于相对布局点击
  • new TranslateAnimation(0f, 0f, 0, -40) 将使您的按钮从当前位置动画到顶部 40 像素。但是您声称“来自顶部并进入其位置”。看不出这是怎么发生的。
  • 对不起,我弄错了。单击相对布局时,动画无论如何都不起作用。

标签: android android-relativelayout translate-animation


【解决方案1】:

将此添加到RelativeLayout

android:clickable="true"

代替:

relativeLayout.setAnimation(animations());

这样做:

relativeLayout.startAnimation(animations());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多