@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl_Root = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
addContentView(rl_Root, lp);
Button btn_Test = new Button(this);
btn_Test.setText("测试");
rl_Root.addView(btn_Test);
btn_Test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 200);
animation.setInterpolator(new AccelerateInterpolator());
animation.setDuration(1000);
animation.setRepeatCount(0);
animation.setFillAfter(true);
final Button btn = (Button)v;
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationEnd(Animation animation) {
btn.setTop(200);
}
public void onAnimationRepeat(Animation animation) {}
});
btn.startAnimation(animation);
}
});
}
换成了绝对布局也是这样,而且我不用动画移动,直接在OnClick中调用 btn.setTop(200);,按钮也看不见了,说明和TranslateAnimation无关
Android 下位置移动是相对的,与布局有关,所以移动一下不见了正常。
如果你想按你指定的位置或坐标移动,你在布局时应该使用:绝对布局,这个每个控件的位置都要以自由移动。