我无法弄清楚为什么当我点击下面代码中的“toactility”按钮时,overridePendingTransition
根本没有响应,而是我收到了默认的淡入淡出动画。这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information_navi);
Button ToMainActivity = (Button)findViewById(R.id.toactivity);
ToMainActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent startIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(startIntent);
overridePendingTransition(0, R.anim.navigation_slide_left);
finish();
}
});
}
我尝试将finish();
放在startActivity(startIntent);
及其下方的不同位置。 overridePendingTransition
被忽略。
但是,在Android硬后退按钮上,overridePendingTransition
按预期工作。这是代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void finish() {
super.finish();
overridePendingTransition(0, R.anim.navigation_slide_left);
}
无法弄清楚为什么overridePendingTransition
代码不能低于或高于startActivity(startIntent);
如何让它发挥作用?
答案 0 :(得分:0)
从here where I have answered开始,使用如下:
overridePendingTransition(R.anim.enter_from_right, R.anim.exit_out_left);
然后在res->anim
文件夹中:
enter_from_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>
</set>
exit_out_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0%"
android:toXDelta="-100%" >
</translate>
</set>