将布局分为两个活动片段

时间:2013-08-23 09:16:04

标签: android android-layout android-fragments admob

我想将我的布局分为两部分: 广告的底部,应用程序的另一部分(少数活动)。 为什么我想这样做?因为我希望每次即使新活动开始时也会出现广告。 http://i.stack.imgur.com/plVeO.png 我意识到可以用片段做到这一点。  我对片段的问题很少:  片段可以举行两个活动?  我可以用意图开始像活动这样的片段吗?


主要问题是如何在使用片段时将布局分离到部件中?

2 个答案:

答案 0 :(得分:2)

不,片段不能有两个活动,反之亦然 - 是的。对于带有片段的动态操作,创建xml文件,其中2个帧,一个附加到底部,并阅读http://developer.android.com/guide/components/fragments.htmlhttp://developer.android.com/reference/android/app/FragmentManager.htmlhttp://developer.android.com/reference/android/app/FragmentTransaction.html

试试这个:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<FrameLayout
    android:id="@+id/frame1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >
</FrameLayout>

<FrameLayout
    android:id="@+id/frame2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="3" >
</FrameLayout>

</LinearLayout>

在活动中:

    NewFragment fragment = new NewFragment();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.frame1, fragment);
    ft.commit();

或者,xml中的静态:

<LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

<fragment android:name="com.example.android.fragments.HeadlinesFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

<fragment android:name="com.example.android.fragments.ArticleFragment"
          android:id="@+id/article_fragment"
          android:layout_weight="3"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

</LinearLayout>

答案 1 :(得分:0)

你可以创建一个父级活动来保存广告并拥有内容的可用空间,而不是继承该活动并从继承的活动中填充内容,也可以在基础中扩展其他布局。如果您将使用多个内容布局,您也可以制作父布局并“继承”它。 或者您可以使用一个活动来保存广告并使用片段作为内容。

相关问题