How to bring view in front of everything?

后端 未结 16 2223
刺人心
刺人心 2020-11-30 01:31

I have activity and a lot of widgets on it, some of them have animations and because of the animations some of the widgets are moving (translating) one over another. For exa

16条回答
  •  孤独总比滥情好
    2020-11-30 01:52

    bringToFront() is the right way, but, NOTE that you must call bringToFront() and invalidate() method on highest-level view (under your root view), for e.g.:

    Your view's hierarchy is:

    -RelativeLayout
    |--LinearLayout1
    |------Button1
    |------Button2
    |------Button3
    |--ImageView
    |--LinearLayout2
    |------Button4
    |------Button5
    |------Button6
    

    So, when you animate back your buttons (1->6), your buttons will under (below) the ImageView. To bring it over (above) the ImageView you must call bringToFront() and invalidate() method on your LinearLayouts. Then it will work :) **NOTE: Remember to set android:clipChildren="false" for your root layout or animate-view's gradparent_layout. Let's take a look at my real code:

    .xml

    
    
    
        
        
    
        
    
            
            
    
            
    
            
            
        
    
    
    

    Some code in .java

    private LinearLayout layoutTop, layoutBottom;
    ...
    layoutTop = (LinearLayout) rootView.findViewById(R.id.layout_top);
    layoutBottom = (LinearLayout) rootView.findViewById(R.id.layout_bottom);
    ...
    //when animate back
    //dragedView is my layoutTop's child view (i added programmatically) (like buttons in above example) 
    dragedView.setVisibility(View.GONE);
    layoutTop.bringToFront();
    layoutTop.invalidate();
    dragedView.startAnimation(animation); // TranslateAnimation
    dragedView.setVisibility(View.VISIBLE);
    

    GLuck!

提交回复
热议问题