How do I get rid of this black bar during animatio

2019-02-11 01:00发布

问题:

I'm trying to perform a custom animation on an Activity transition. The activity is supposed to slide smoothly upward instead of from the side. It works, but the view has a black bar (the same size as the status bar) on the top of the view. How can I get rid of the black bar? Here is the code I'm using:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  overridePendingTransition(R.anim.upin, R.anim.upout);
  setContentView(R.layout.screen_login);
}

the animation upin:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="100%" android:toYDelta="0%" android:duration="500" />
</set>

the animation upout:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0%" android:toYDelta="0%" android:duration="500" />
</set>

EDIT: If I make all the Activities fullscreen it works perfectly. Of course, I don't WANT all the Activities to be fullscreen.

回答1:

Okay, found a way of doing this. In the activity that you are animating to, override your onCreate method. Before you call setContentView (I don't know if this is necessary, but it's the order in which I did it), add this code snippet:

ColorDrawable colorDrawable = new ColorDrawable( Color.TRANSPARENT );
getWindow().setBackgroundDrawable( colorDrawable );

This worked like a charm for me and I haven't seen any other side effects as of yet.

I actually ran into the same issue and stumbled upon your question. Hope you find this helpful.

  • groomsy

EDIT: Tested on Nexus One running 2.3.4



回答2:

Okay, I tried a few things, like making the deltas relative to the parent, etc. I think the core problem is that the activity has extra space at the top, which is that black bar. There's not really anything to be done about it as far as I can see except to pull it out of the activity and make it a view.

Placing that view into a ViewAnimator and then setting the in and out transitions to those I already listed works like a charm. I'm fairly convinced that there is no other way to do this. If anyone ever figures one out, let me know, but this solves my problem.



回答3:

I know this question is pretty old, but I ran into the same problem (black bar/space at the top of the activity) and this fixed it for me:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    setContentView(R.layout.your_layout);
}

Hopefully this will help somebody who stumbles upon this question :)



回答4:

Solution is here to prevent black background while overridePendingTransition() https://stackoverflow.com/a/49751078/5423596