如何在Linearlayout中添加广告

时间:2014-02-04 12:19:44

标签: android admob

我正在尝试将AdMob广告添加到我的应用程序中。广告显示在屏幕底部,但是当它显示我的最后几行时,我会隐藏广告。我可以做什么来自动升级当前布局并显示广告。我的代码是

Window window1 = getWindow();
LinearLayout adsLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT);
adsLayout.setGravity(Gravity.BOTTOM);
adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxx");  
adsLayout.addView(adView);
AdRequest newAdReq = new AdRequest();
adView.loadAd(newAdReq);
window1.addContentView(adsLayout,lp2);

我的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ScienceActivity"
    android:id="@+id/addlayout"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="@string/science" />
<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#000000"
    android:paddingTop="20dp" />

    <EditText
        android:id="@+id/textbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="10"
        android:singleLine="false"
        android:editable="false"
        android:focusable="false"
        android:clickable="false"
        android:text="@string/Sciencearticle" />
    <View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#000000"
      android:paddingTop="20dp"
  />

    <Button 
        android:id="@+id/article"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="5dp"
        android:typeface="serif"
        android:background="#55ee00"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="articleClicked"
        android:text="@string/article"
        />

     <Button 
        android:id="@+id/times_article"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
         android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:typeface="serif"
        android:background="#55ee00"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="timesArticleClicked"
        android:text="@string/times_article"/>
      <LinearLayout
        android:id="@+id/addlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"        
        android:weightSum="1" >


        </LinearLayout>





</LinearLayout>

2 个答案:

答案 0 :(得分:2)

您可以按照以下方式执行

<强> XML

<LinearLyaout MAIN 
     Height:fillparent
     Height:fillparent >

     <your main content area
        Height:fillparent
        Height:fillparent 
        weight:1>
     </your main content area>
     <Linearlayout 
          id:+@llAdd
          width:fillparent
          Height:wrapcontent/>
</LinearLayout>

<强> JAVA

LinearLayout adsLayout = (LinearLayout)findviewbyid(R.id.llAdd);
adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxx");  
adsLayout.addView(adView);
AdRequest newAdReq = new AdRequest();
adView.loadAd(newAdReq);

EDITED XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/addlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ScienceActivity" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="science"
            android:textSize="25sp" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#000000"
            android:paddingTop="20dp" />

        <EditText
            android:id="@+id/textbox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:editable="false"
            android:ems="10"
            android:focusable="false"
            android:gravity="top"
            android:inputType="textMultiLine"
            android:lines="10"
            android:singleLine="false"
            android:text="Sciencearticle" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#000000"
            android:paddingTop="20dp" />

        <Button
            android:id="@+id/article"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:background="#55ee00"
            android:gravity="center"
            android:onClick="articleClicked"
            android:text="article"
            android:textColor="#ffffff"
            android:textSize="25sp"
            android:typeface="serif" />

        <Button
            android:id="@+id/times_article"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="#55ee00"
            android:gravity="center"
            android:onClick="timesArticleClicked"
            android:text="times_article"
            android:textColor="#ffffff"
            android:textSize="25sp"
            android:typeface="serif" />
    </LinearLayout>
</ScrollView>
    <LinearLayout
        android:id="@+id/addlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        android:weightSum="1" >
    </LinearLayout>

</LinearLayout>

答案 1 :(得分:0)

这可以帮助你:

LinearLayout adContainer = <container>;
AdView adView = new AdView(activity, com.google.ads.AdSize.BANNER, <publisher_id>);

// Initiate a generic request to load it with an ad
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);

// Place the ad view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
adContainer.addView(adView, params);