将一个活动覆盖在另一个活动上,或者将一个视图覆盖在另一个活动上



我有两个类,FirstActivity和SecondActivity。

第一次活动

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

是否有可能为SecondActivity叠加在FirstActivity上?ie。FirstActivity变暗,SecondActivity显示在FirstActivity的顶部。

如果它是不可能的2个不同的活动,是否有可能做一个覆盖2个视图在同一活动?我希望使用对话框不是唯一的选择。

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

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

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

或者你可以简单地在manifest中设置主题作为你的SecondActivity的对话框

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

<?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)是你的overlay布局,你想要显示在基础布局的顶部。背景颜色会给你褪色的背景,你可以添加任何你想要的布局,使你的覆盖。要显示叠加,只需将其可见度从gone更改为visible

在manifest文件中像这样声明第二个activity activity。android:主题="@android:风格/Theme.Dialog"。然后从代码中调用firstactivity中的第二个activity。

 <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>

第二个活动xml文件。你可以按照你的意愿设计,但作为参考,我已经发布了这个。关键的概念是在manifest文件(即)如何定义你的第二个活动在manifest

    <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>

1-截取第一个活动的截图

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

3-然后调用第二个活动,并使用第一个活动的截图作为第二个活动的背景

最新更新