附加到父 (RelativeLayout) 布局时动画不起作用?
Animation doesn't work when attached to the parent (RelativeLayout)layout?
我尝试将按钮从顶部动画化到屏幕上的固定位置,当在按钮上添加 clicklistener 时它起作用了,但在附加到 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>
将此添加到 RelativeLayout
:
android:clickable="true"
而不是:
relativeLayout.setAnimation(animations());
这样做:
relativeLayout.startAnimation(animations());
我尝试将按钮从顶部动画化到屏幕上的固定位置,当在按钮上添加 clicklistener 时它起作用了,但在附加到 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>
将此添加到 RelativeLayout
:
android:clickable="true"
而不是:
relativeLayout.setAnimation(animations());
这样做:
relativeLayout.startAnimation(animations());