在先前的 Activity 之上显示新的 Activity,但仅部分覆盖先前的 Activity

发布于 2024-12-21 15:16:51 字数 388 浏览 0 评论 0原文

所需行为的流程

我有一个活动 A,它有一个“向下”按钮(参见上图)。当我按下此按钮时,我想在 Activity A 之上启动 Activity B,以便底部的 Activity A 的一些内容仍然可见。 Activity B 可以依次启动许多其他 Activity(C、D 等),以便它们仅显示在与 B 相同的屏幕区域内。有谁知道这是否可能,如果可以,我该怎么做?

编辑

我知道我可以将 android:theme="@android:style/Theme.Dialog" 添加到我的 Activity 的清单声明中。但是,据我所知,它显示在屏幕中央,并且无法根据需要调整其大小。如果我错了请指正

Flow of desired behaviour

I have an Activity A which has a "down" button (see image above). When I press this button, I want to launch an Activity B on top of Activity A such that some of Activity A at the bottom is still visible. Activity B can in turn launch a number of other Activities (C, D etc) such that they only display within the same screen area as B. Does anybody know whether this is possible and, if so, how I can go about it?

Edit

I am aware that I can add android:theme="@android:style/Theme.Dialog" to my Activity's manifest declaration. However, as far as I am aware, this displays in the centre of the screen and not such that I can resize it how I require. Correct me if I am wrong ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∞梦里开花 2024-12-28 15:16:51

这是应该位于活动 A 之上的活动的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00000000" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0"
        android:layout_weight="0.6"
        android:background="#000000" >
        //use this for your new activity contents
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0"
        android:layout_weight="0.4"
        android:background="#00000000" >
        //this is empty
    </LinearLayout>
</LinearLayout>

现在,在活动 B、C 等的 ma​​nifest 中:

<activity
    android:label="@string/app_name"
    android:name=".ActivityB"
    android:theme="@style/Theme.Transparent"
    android:screenOrientation="portrait" >

values/styles.xml 中:

<?xml version="1.0" encoding="utf-8"?>  
<resources>
    <style name="Theme.Transparent" parent="android:Theme.NoTitleBar">  
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@drawable/theme_transparent</item>
    </style>
</resources>  

最后,在drawable/theme_transparent.xml中:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#00000000" ></solid>
</shape>

This is the XML for activities that should be on top of Activity A:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00000000" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0"
        android:layout_weight="0.6"
        android:background="#000000" >
        //use this for your new activity contents
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0"
        android:layout_weight="0.4"
        android:background="#00000000" >
        //this is empty
    </LinearLayout>
</LinearLayout>

Now, in your manifest for Activities B, C etc:

<activity
    android:label="@string/app_name"
    android:name=".ActivityB"
    android:theme="@style/Theme.Transparent"
    android:screenOrientation="portrait" >

In values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>  
<resources>
    <style name="Theme.Transparent" parent="android:Theme.NoTitleBar">  
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@drawable/theme_transparent</item>
    </style>
</resources>  

Finally, in drawable/theme_transparent.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#00000000" ></solid>
</shape>
夜未央樱花落 2024-12-28 15:16:51

你需要使用的是 Fragments。

Fragments 可以用来填充屏幕的一部分,同时在完全不同的屏幕中执行其他操作。
在您的示例中,您可以创建一个包含两个 Fragments 的主活动。一个 Fragment 控制“A 活动”,另一个控制“B 活动”等等。
通过按下按钮将“B 活动”区域中的当前 Fragment 替换为不同的 Fragment,您就可以实现您正在寻找的行为。至少,我在包含主要内容区域和音乐播放器的应用程序中就是这样做的。当主要内容发生变化时,音乐播放器保持不变。

遗憾的是,我现在无法提供任何示例代码,但这里有一个教程可以帮助您入门:

如果您使用的是 3.0 以下的 Android 版本,则可以使用兼容性包。

http://developer.android.com/sdk/compatibility-library.html

http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/

What you need to use, are Fragments.

Fragments can be used to fill a part of the screen, while doing something else entirely in a different one.
In your example you can create a main activity that contains two Fragments. One Fragment controls the "A activity" , the other one controls the "B activity" and the other ones..
By replacing the current Fragment in your "B activity" area with a different one on the press of a button, you can achieve the behavior you are looking for. At least, that's how I did it in an app of mine containing a main content area and a music player. The music player stays in place while the main content changes.

Sadly I can't provide any example code right now, but here is a tutorialthat should help you get started:

In case you are working with an Android version below 3.0, you can use the Compatibility package.

http://developer.android.com/sdk/compatibility-library.html

http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/

如梦 2024-12-28 15:16:51

关于“编辑”,实际上,您可以移动/调整对话框主题活动的大小。
例如,要使您的 Dialog 主题活动显示在屏幕顶部而不是默认的中心,并且占屏幕宽度的 80%,只需在 Dialog 主题活动中的 setContentView 之后添加以下内容:

Window window = getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP;
window.setAttributes(wlp);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = (int) (metrics.widthPixels * 0.80);
window.setLayout(screenWidth, WindowManager.LayoutParams.WRAP_CONTENT);

Regarding your "Edit", actually, you can move around / resize a Dialog themed activity.
For example, to make your Dialog themed activity appear at the top of the screen instead of the default center and be 80% width of the screen, just add the following after setContentView in your Dialog themed activity:

Window window = getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP;
window.setAttributes(wlp);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = (int) (metrics.widthPixels * 0.80);
window.setLayout(screenWidth, WindowManager.LayoutParams.WRAP_CONTENT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文