添加行/图像上面动作条 [英] Add a Line/Image above ActionBar

查看:115
本文介绍了添加行/图像上面动作条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加上述就像在口袋-app操作栏线。我怎样才能做到这一点?

I want to add a line above the action bar like in the "pocket"-app. How can i do this?

下面是例如图片:

Here is a picture for example:

谢谢 TomTom公司

Thanks tomtom

推荐答案

以一个活动的窗口管理器的优势,我们可以借鉴,我们要在上面的任何视图。下面是一些(半准)code,应该帮助:

Taking advantage of an Activity's WindowManager, we can draw any view we want on top. Here's some (half-pseudo) code that should help:

// Create an instance of some View that does the actual drawing of the line
View customView = new CustomView(<some context>);

// Figure out the window we have to work with
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

// Make sure the view is measured before doing this
int requestedHeight = customView.getLayoutParams().height;

// setup the params of the new view we'll attach
WindowManager.LayoutParams wlp = new WindowManager.LayoutParams(
     rect.width(), requestedHeight,
     WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
     WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
          WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
          WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
     PixelFormat.TRANSLUCENT);
// set the parameters so we fit on the top left of the window
wlp.x = 0;
wlp.y = rect.top;
wlp.gravity = Gravity.TOP;

// finally add it to the screen
getWindowManager().addView(header, wlp);

要小心的唯一的事情是,你不能运行的onCreate(即code)或因为窗口将不会被创建活动的任何生命周期方法(你会得到一个BadTokenException) 。一种方式是发布可运行在窗口的DecorView让您的code添加CustomView窗口创建后运行:

The only thing to be careful is that you can't run that code from onCreate() or any lifecycle method of the Activity because the Window won't have been created yet (You'll get a BadTokenException). One way might be post a Runnable on the Window's DecorView so that your code to add the CustomView runs after the Window is created:

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     //...
     getWindow().getDecorView().post(<Runnable that execs code above>);
 }

至于实际的 CustomView 的,将显示多种颜色的吧,我觉得这是一个很好的锻炼:-) 所有你需要做的就是拥有的OnDraw()方法使用canvas.drawRect()与特定的x和宽度。

As for the actual CustomView that will display that multi-coloured bar, I feel like that's a good exercise :-) All you'd need to do is have the onDraw() method use canvas.drawRect() with specific x and widths.

希望有所帮助。

什么掌上确实

至于掌上实际上是怎么做的。如果您使用 HierarchyViewer 上的Pocket应用程序,你就可以确定掌上使用自定义类的动作条。既然他们已经重建动作条的所有功能,满足他们的需要,在他们的情况下,加入该行中,就像将定期查看一些ViewGroup中。

As for how Pocket actually does it. If you use HierarchyViewer on the Pocket app, you'll be able to determine that Pocket uses a custom class for their ActionBar. Since they already rebuild all the features of the ActionBar for their needs, in their case, adding the line is like adding a regular View to some ViewGroup.

这篇关于添加行/图像上面动作条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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