How to bring view on front of everything?

2019-01-10 21:52发布

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 example the text view is moving over some buttons . . .

Now the thing is I want the buttons to be always on the front. And when the textview is moving I want to move behind the buttons.

I can not achieve this I tried everything I know, and "bringToFront()" definitelly doesn't work.

note I do not want to control the z-order by the order of placing element to layout cause I simply can't :), the layout is complex and I can not place all the buttons at the begging of the layout

12条回答
Melony?
2楼-- · 2019-01-10 22:15

Try FrameLayout, it gives you the possibility to put views one above another. You can create two LinearLayouts: one with the background views, and one with foreground views, and combine them using the FrameLayout. Hope this helps.

查看更多
够拽才男人
3楼-- · 2019-01-10 22:16

You need to use framelayout. And the better way to do this is to make the view invisible when thay are not require. Also you need to set the position for each and every view,So that they will move according to there corresponding position

查看更多
来,给爷笑一个
4楼-- · 2019-01-10 22:18

An even simpler solution is to edit the XML of the activity. Use

android:translationZ=""
查看更多
聊天终结者
5楼-- · 2019-01-10 22:23

You can try to use the bringChildToFront, you can check if this documentation is helpful in the Android Developers page.

查看更多
Evening l夕情丶
6楼-- · 2019-01-10 22:23

You can set visibility to false of other views.

view1.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
...

or

view1.setVisibility(View.INVISIBLE);
view2.setVisibility(View.INVISIBLE);
...

and set

viewN.setVisibility(View.VISIBLE);
查看更多
Evening l夕情丶
7楼-- · 2019-01-10 22:26

i have faced the same problem. the following solution have worked for me.

 FrameLayout glFrame=(FrameLayout) findViewById(R.id.animatedView);
        glFrame.addView(yourView);
        glFrame.bringToFront();
        glFrame.invalidate();
查看更多
登录 后发表回答