Android跨活动浮动视图 [英] Android floating view across activities

查看:136
本文介绍了Android跨活动浮动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中进行了 2个活动(例如A和B ),并具有A->BB->A之类的导航(按返回键).我的要求是

I have an application with 2 activities, say A and B, with navigations like A->B and B->A (on back press). My requirement is

  • 我希望在屏幕上浮动视图/布局,无论哪个 活动当前可见.我在应用程式上膨胀了这种观点 start(活动A的onCreate),在活动期间它在屏幕上保持静态 从A-> B过渡到B出现在屏幕上.
  • 因此,此视图自然应该只膨胀一次(当应用启动时, onCreate).
  • I want a view/layout floating on screen, irrespective of which activity is currently visible. I inflate this view on app start(onCreate of activity A), it remains static on screen during the transition from A->B and when B is onscreen.
  • So naturally this view should be inflated only once (when app starts, onCreate of A).

我做了一些搜索,从我发现的东西中,有2种方法可以在android中重新使用布局

I did some searching, and from what I could find, there are 2 methods to reuse layout in android

  • 使用 <include>

这似乎是一种编写常用UI元素的xml代码的工具.每次在父级布局中使用它时,它都会膨胀.

It just seems like a tool to write xml code of commonly used UI elements. It gets inflated every time it is used in a parent layout.

使用 ViewStub

我对使用ViewStub进行了一些研究,这似乎也是一种在许多布局中重用代码段的方法.每次我们在布局中使用它时,都需要对其进行充气,除非它仅在运行时使它们可见时才被充气.

I did some research on using ViewStub and It also seems a way to reuse code segment in many layouts. It also need to be inflated every time we use it in a layout except it gets inflated only when we make them visible at run time.

对于熟悉iPhone开发的人们,您可以将视图添加到UIWindow中,无论当前处于活动状态的UIViewController都在该视图中.我想要我的应用程序中的确切行为.

For people familiar with iPhone development you can add view's to UIWindow, which stays there irrespective of which UIViewController is currently active. I want exact behavior in my application.

我的目标是android 2.1及更高版本.看来Fragment在API级别11(android 3.0)及更高版本中可用.一种选择是使用 Android兼容性库,该库可以在较早的版本中使用Fragment版本.我目前正在对此进行研究.但是我也想知道是否还有其他方法可以满足我的要求,而不是更改整个项目并使用片段.

I am targeting android 2.1 and above. It seems Fragment is available from API level 11 (android 3.0) and above. One option is to use android compatibility library that enables usage of Fragment in older versions. I am currently researching on that now. But I also would like to know if there is any other methods available to fulfill my requirement, rather than change my entire project and use fragments.

我的应用程序中大约有30个奇怪的活动,我希望此布局浮动在所有这些活动上.我只是用两个活动制定了一个测试用例,以使问题变得简单而简单.

I have around 30 odd activities in my application and I want this layout floating over all of them. I just made out a test case with 2 activities to make the question simple and easy.

推荐答案

解决方案1:FrameLayout

我认为您要使用的是FrameLayout. FrameLayout旨在遮挡屏幕上的某个区域以显示单个项目.子视图以堆栈形式绘制,最近添加的子视图位于顶部.

Solution 1: FrameLayout

I think what you want to use is the FrameLayout. FrameLayout is designed to block out an area on the screen to display a single item. Child views are drawn in a stack, with the most recently added child on top.

http://developer.android.com/reference/android/widget/FrameLayout.html

然后在此处阅读有关可在活动中使用的后退堆栈的信息,使用后退按钮可以在活动之间来回切换:

Then read here about the back stack that you could use in your activity to flip back and forth between the activities using the back button:

http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

与其编写两个单独的活动,不如编写具有两个片段的单个活动.以下是片段文档中的摘要:

Rather than code two separate Activities, code a single Activity with two Fragments. Here is a blurb from the Fragments documentation:

"片段必须始终嵌入到活动中,并且片段的生命周期直接受到宿主活动的生命周期的影响.例如,当活动被暂停时,其中的所有片段以及活动被销毁时也是如此,因此都是片段.但是,当一个活动正在运行时(处于生命周期的恢复状态),您可以独立地操作每个片段,例如添加或删除它们.将其返回到由活动管理的后退堆栈 –活动中的每个后退堆栈条目都是发生的片段事务的记录.后退堆栈允许用户通过按以下操作来反转片段事务(向后导航)后退按钮."

"A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button."

这篇关于Android跨活动浮动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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