如何将静态AdMob代码转换为动态代码

时间:2013-02-01 07:32:05

标签: android

我正在将 AdMob 添加到我的应用中。显然我需要将以下布局片段添加到我的每个布局中:

 <LinearLayout
     android:id="@+id/linearLayout"
     android:orientation="vertical"
     android:layout_below="@+id/button2"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" />

但是,我宁愿动态添加上面的代码段,而不是将其复制粘贴到每个布局中。

我遇到的问题是翻译以下代码段:

android:layout_below="@+id/button2"

到Java代码中。

1 个答案:

答案 0 :(得分:1)

您有两种选择(至少):

  1. 在XML上删除android:layout_below="@+id/button2"并在代码上添加此属性,就像在这个问题上说的那样: How to lay out Views in RelativeLayout programmatically?
  2. 另一个选项是创建一个xml布局,其中只包含您在问题上发布的代码,而不包含android:layout_below="@+id/button2"。然后,在要使用此布局的每个布局中,使用include元素:http://developer.android.com/training/improving-layouts/reusing-layouts.html
  3. 希望它有所帮助。