在操作栏顶部显示视图 [英] display view on top of action bar

查看:21
本文介绍了在操作栏顶部显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在操作栏顶部呈现视图?我想创建一个小提示框,将用户指向操作栏中的一个项目.我知道带有设置视图的 Toast 将呈现在操作栏上方.有谁知道如何用视图来做到这一点?

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?

我已尝试将 FrameLayoutlayout_gravity="top" 结合使用,然后对视图进行充气,然后将其添加到正在运行的 Activity 的布局中.

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

我提前感谢你.

编辑:这是我在想的图像:

编辑:也许需要更多的细节.我正在寻找一种方法,或者找出是否甚至可以将视图添加到活动的视图层次结构中,以便最后呈现它.

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.

与 CSS 类似,我希望此特定视图(图像中的蓝色浮动框)具有更高的 z-index 顺序,以便将其呈现在活动中的操作栏区域的顶部.视图与操作栏没有任何关联,它只是简单地绘制在它之上.

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.

推荐答案

在自己挣扎了很长时间之后,这里的解决方案(经过测试 - 运行良好):

After struggling with it myself quite some time, here's the solution (tested it - working good):

一般步骤是:

  1. 创建包装视图
  2. 分离屏幕视图子项,放置包装器,并附加子项
  3. 向孩子们展示内容
  4. 控制包装器将帮助您完全控制操作栏及其下方的内容.
  5. 现在,使用包装器,您可以将兄弟"添加到操作栏/主区域.那个兄弟正是你在图片中所描述的.

让我们看看一些代码.

首先,创建一个方法来帮助创建一个包装视图.包装器将放置在整个屏幕和您的应用程序内容之间.作为 ViewGroup,您以后可以完全控制它的内容.

First, create a method to help create a wrapper view. the wrapper will be placed between the entire screen and the content of your app. being a ViewGroup you can later on fully control it's content.

private ViewGroup setContentViewWithWrapper(int resContent) {
        ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
        ViewGroup decorChild = (ViewGroup) decorView.getChildAt(0);

        // Removing decorChild, we'll add it back soon
        decorView.removeAllViews();

        ViewGroup wrapperView = new FrameLayout(this);

        // You should set some ID, if you'll want to reference this wrapper in that manner later
        //
        // The ID, such as "R.id.ACTIVITY_LAYOUT_WRAPPER" can be set at a resource file, such as:
        //  <resources xmlns:android="http://schemas.android.com/apk/res/android">
        //      <item type="id" name="ACTIVITY_LAYOUT_WRAPPER"/>
        //  </resources>
        //
        wrapperView.setId(R.id.ACTIVITY_LAYOUT_WRAPPER);

        // Now we are rebuilding the DecorView, but this time we 
        // have our wrapper view to stand between the real content and the decor
        decorView.addView(wrapperView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        wrapperView.addView(decorChild, decorChild.getLayoutParams());
        LayoutInflater.from(this).inflate(getActivityLayout(), 
                    (ViewGroup)((LinearLayout)wrapperView.getChildAt(0)).getChildAt(1), true);

        return wrapperView;
    }

现在,干扰常规的 Activity 创建,而不是使用 setContentView,使用我们创建的方法.

Now, interfere with the regular Activity creation, and instead of using setContentView, use the method we've created.

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

    // DON'T CALL `setContentView`, 
    // we are replacing that line with this code:
    ViewGroup wrapperView = setContentViewWithWrapper(R.layout.activity_layout);

    // Now, because the wrapper view contains the entire screen (including the notification bar
    // which is above the ActionBar) I think you'll find it useful to know the exact Y where the 
    // action bar is located.
    // You can use something like that:
    ViewGroup actionBar = (ViewGroup)((LinearLayout)wrapperView.getChildAt(0)).getChildAt(0);
    int topOffset = actionBar.getTop();

    // Now, if you'll want to add a view:
    //  1. Create new view
    //  2. Set padding top - use "topOffset"
    //  3. Add the view to "wrapperView"
    //  4. The view should be set at front. if not - try calling to "bringToFront()"
}

就是这样.

注意事项

  • 我使用 Android 的层次结构查看器来了解什么是正确的层次结构.(没猜到那些 01 索引)
  • 如果您在活动中使用某种菜单抽屉,您可能需要对其进行一些不同的配置,因为抽屉已经为您创建了该包装器
  • 通过查看这个很棒的库
  • ,我学到了很多东西
  • I've used Android's hierarchy-viewer to understand what's the right hierarchy. (didn't guess those 0 and 1 indexes)
  • If you are using some kind of a menu drawer in your activity, you might have to configure it a little bit different since drawers are already creating that wrapper for you
  • I've learned a lot by looking at this great library

请参阅@CristopherOyarzúnAltamirano 答案以获得对较新 Android 版本的进一步支持

Refer to @CristopherOyarzúnAltamirano Answer for further support on newer Android versions

祝你好运!

这篇关于在操作栏顶部显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆