【问题标题】:Overlay an activity on another activity OR overlay a view over another将一个活动覆盖在另一个活动上或将一个视图覆盖在另一个活动上
【发布时间】:2011-12-14 06:16:46
【问题描述】:

我有 2 个类,FirstActivity 和 SecondActivity。

第一个活动

Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
startActivity(intent);

SecondActivity 是否可以覆盖在 FirstActivity 上? IE。 FirstActivity 变暗,SecondActivity 显示在 FirstActivity 之上。

如果不能进行 2 个不同的活动,是否可以在同一个活动中为 2 个视图进行叠加?我希望使用对话框不是唯一的选择。

【问题讨论】:

    标签: android view android-activity overlay


    【解决方案1】:

    我建议您将您的第二个活动设置为一个对话框——这会使背景变暗。这是一个可能有用的教程:

    http://developer.android.com/guide/topics/ui/dialogs.html

    http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application

    或者您可以简单地将清单中的主题设置为您的 SecondActivity 的对话框。

    【讨论】:

    • 是的,我知道对话框是一种选择,但有更好的方法吗?在 UI 方面,它看起来不太好。
    • 当然,您可以为内容创建视图并根据需要调整 alpha。我不太确定你在这里做什么,通常 FirstActivity 根本看不到,因为 SecondActivity 会覆盖屏幕?
    • 我的第二个活动更像是一个EditText 框,因此它只会覆盖屏幕的一半。这也是为什么我正在考虑将 SecondActivity 合并到 FirstActivity 中,看看我是否可以做一个View 覆盖。 SecondActivity 就像一个子活动,我希望用户快速回到 FirstActivity
    • 您始终可以创建自定义对话框... 一个活动代表具有用户界面的单个屏幕。例如,一个电子邮件应用程序可能有一个显示新电子邮件列表的活动,另一个用于撰写电子邮件的活动,以及另一个用于阅读电子邮件的活动。尽管这些活动一起工作以在电子邮件应用程序中形成一种有凝聚力的用户体验,但每个活动都独立于其他活动。因此,不同的应用程序可以启动这些活动中的任何一项(如果电子邮件应用程序允许的话)。
    • 另外,如果您不喜欢对话框的主题,您可以随时创建自己的主题。
    【解决方案2】:

    如果你不想做一个对话框,你可以使用相对布局覆盖视图。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <LinearLayout android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView android:id="@+id/text"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="some content"
                android:textSize="70dp"/>
        </LinearLayout>
    
        <LinearLayout android:id="@+id/overlay"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#99000000"
                android:clickable="true"
            android:visibility="gone">
            <EditText android:id="@+id/edittext"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_margin="50dp" />
        </LinearLayout>
    
    </RelativeLayout>
    

    第一个 LinearLayout (id/content) 是你的基本布局,你的正常内容会去哪里。

    第二个 LinearLayout (id/overlay) 是您想要显示在基本布局之上的叠加布局。背景颜色将为您提供淡出的背景,您可以在该布局中添加任何您想要的内容来制作叠加层。要显示叠加层,只需将其可见性从 gone 更改为 visible

    【讨论】:

    • 是不是意味着第一个线性布局中的按钮等在覆盖布局可见后仍然可以完全发挥作用?
    • 如果不希望背景可点击,可以让叠加层 LinearLayout 可点击。这将捕获所有点击并防止它们在覆盖层可见时进入背景。当您将叠加层的可见性设置为“已消失”时,内容层的可点击性将恢复。我更新了代码示例以显示这一点。
    • 您的解决方案对我来说就像一个魅力。我只想说,如果你想测试更多的背景颜色(不透明度代码),你可以看到这个很好的答案:stackoverflow.com/a/16890937/4744263
    【解决方案3】:

    在清单文件中像这样声明 secondactivity 活动。 android:theme="@android:style/Theme.Dialog"。然后只需从您的代码中调用 firstactivity 中的 secondactivity。

     <activity
                    android:name=".FirstActivity"
                    android:label="@string/title_activity_first" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
        
                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
                <activity
                    android:name=".SecondActivity"
                    android:label="@string/title_activity_second" 
                    android:theme="@android:style/Theme.Dialog"
                    >
                    <intent-filter>
                        <action android:name="transparent.text.SECONDACTIVITY"/>
        
                        <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
                </activity>
    

    Second Activity xml 文件。您可以根据自己的意愿进行设计,但作为参考,我已发布此内容。关键概念在 manifestfile(即)如何在 manifest 中定义您的 secondactivity

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="192dp"
                android:background="#aabbcc"
                android:text="Sybrant has provided Takoma with a great team which helped us from the beginning to the final stage of our product, to our fullest satisfaction. We have been able to deliver a high quality of eLearning products to our corporate customers like Nissan with Sybrant’s support”"
                tools:context=".FirstActivity" />
        
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/textView1"
                android:layout_alignParentLeft="true"
                android:layout_marginBottom="43dp"
                android:layout_marginLeft="80dp"
                android:text="Button" />
        
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/button1"
                android:layout_below="@+id/textView1"
                android:layout_marginRight="42dp"
                android:layout_marginTop="80dp"
                android:text="TextView" />
        
        </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      1-对第一个活动进行截图。

      2-(可选)使屏幕截图变暗、着色或模糊。

      3-然后调用第二个activity,将第一个activity截图作为第二个activity的背景。

      【讨论】:

      • 这很苛刻,但我笑了
      • 这在游戏中实际上是一个非常好的策略,当你想要打开一个背景部分可见的全屏菜单时......大量的绘制调用节省!
      • @5argon 这真的取决于你的游戏是什么样子的。仍然 - 在移动应用程序中进行吗?您仍然需要将该图像存储在某个地方,可能是在内存中?你如何处理方向变化,你拍两张截图吗?总的来说,我认为处理这些情况的方法比截屏更好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多