display view on top of action bar

2019-01-21 00:29发布

Is there a way to render a view on top of the action bar? I want to create a small tip box that will point the user to an item in the action bar. I know that a Toast with a set view will be rendered above the action bar. Does anyone know how to do this with a view?

I have attempted using FrameLayout with layout_gravity="top" and inflating a view and then adding it to the running activity's layout.

I appreciate you in advance.

Edit: Here is an image of what I was thinking: enter image description here

Edit: Perhaps some more detail is needed. I am looking for a way, or to find out if it is even possible to add a view to the view hierarchy of the activity so that it is rendered last.

Similar to CSS, I want a higher z-index order for this particular view ( the blue floating box in the image), such that it would be rendered on top of the Action Bar region in the activity. The view is in no way associated with Action Bar, it is simply drawn on top of it.

9条回答
爷、活的狠高调
2楼-- · 2019-01-21 01:17

I found this workaround based on @Sean answer:

        //This is for Jelly, ICS, Honeycomb
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2){
            LayoutInflater.from(this).inflate(resContent, (ViewGroup)((LinearLayout)wrapperView.getChildAt(0)).getChildAt(1), true);}
        //This is for KitKat and Jelly 4.3
        else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
            LayoutInflater.from(this).inflate(resContent, (ViewGroup) (((ViewGroup) wrapperView.getChildAt(0)).getChildAt(0)), true);}
        //This is for Ginger
        else{
            LayoutInflater.from(this).inflate(resContent, (ViewGroup) ((LinearLayout)((FrameLayout) wrapperView.getChildAt(0)).getChildAt(0)).getChildAt(1), true);}
查看更多
Bombasti
3楼-- · 2019-01-21 01:18

I was trying to achieve something else but I needed a solution similar to this. I needed to draw an opaque layer covering the whole screen, even the action bar--sort of like a dialog. I did so this way:

ViewGroup vg = (ViewGroup)(getWindow().getDecorView().getRootView());
vg.addView(myNewView, params);

this can be used to draw anything anywhere on the screen.

查看更多
叛逆
4楼-- · 2019-01-21 01:19

I found a much simpler way to do this. I just applied android:translationZ="10dp" to the view which I need to be covering the action bar.

I chose 10dp but it can actually be anything you want as long as it is superior to the actionbar's elevation

<ImageView
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:translationZ="10dp"
      android:src="@drawable/potatoe" />

Also, don't worry about Android studio's following warning :

"translationZ can't be used with API<21".

It will be ignored, but you don't really care because the toolbar shouldn't cover your view with APIs inferior to 21.

查看更多
登录 后发表回答