如何打开另一个活动而不会丢失主要活动布局上的项目?

问题描述 投票:0回答:2

我有两个空的活动名称主要活动和另一个活动。它们都有不同的内容。在主要活动布局上,我有一个按钮和一个横幅广告。在我的另一个活动布局上,我有一个填充布局宽度和高度的imageview。

我的问题是,如何在主布局上用我的内容页面替换另一个布局的内容页面。所以我不会再次将广告放在另一个活动布局中,并在java类中再次编写admob java代码。

主要活动xml布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     //start content page//
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal|center_vertical"
      android:id="@+id/button/>
      //end content page//


    //the ads
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height"wrap_content"
        android:alignParentBottom="true"/>

</RelativeLayout>

另一个活动是xml布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    //content pages
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height"match_parent"
        android:src="@drawable/pict_two"/>
    //content pages


</RelativeLayout>
android android-layout android-activity
2个回答
0
投票

我建议您使用Fragments,如果您想要可滑动的视图,请尝试使用ViewPager,以便用户只需滑动视图即可导航到两个视图。


0
投票

您可以将ViewGroup与Fragment活动一起使用。

查看API参考https://developer.android.com/reference/android/app/Fragment.html

© www.soinside.com 2019 - 2024. All rights reserved.