xml中的Admob未显示在Linear中

时间:2013-11-11 03:14:54

标签: android xml layout admob

我正在我的应用程序上实现admob,当父级处于相对布局时出现但我不能使用 alignparentbottom 所以我将其更改为线性但是当我更改它时它不显示线性..

任何提示?帮助

这里是xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/banner_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/offline_banner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@color/black"
            android:src="@drawable/offline_banner" />

        <com.google.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/unit_id"
            ads:loadAdOnCreate="true" />
    </RelativeLayout>
 <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

我希望admob位于屏幕的底部,而不使用相对布局的 alignparentbottom

2 个答案:

答案 0 :(得分:0)

不确定为什么要避免使用alignParentBottom,但下面的布局对我有效。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" /> 
   <RelativeLayout
        android:id="@+id/banner_holder"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="bottom"
        android:weight="1" >

            <!-- Everything in here will hug the bottom -->

   </RelativeLayout>
</LinearLayout>

答案 1 :(得分:0)

// try this 
1. download latest google-play-services,jar
2. add jar to project build path

**main.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/lnrMain">

</LinearLayout>

public class MyActivity extends FragmentActivity {

    LinearLayout lnrMain;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lnrMain = (LinearLayout) findViewById(R.id.lnrMain);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AdView adView = new AdView(MyActivity.this);
                adView.setAdUnitId("0445b7141d9d4e1b");
                adView.setAdSize(AdSize.BANNER);
                AdRequest.Builder builder = new AdRequest.Builder();
                builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
                adView.loadAd(builder.build());
                lnrMain.addView(adView);
            }
        });

    }
}

**AndroidManifest.xml**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.Demo"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="8"
        android:targetSdkVersion="17"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher">
        <activity
            android:name="MyActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

        <meta-data
            android:name="ADMOB_ALLOW_LOCATION_FOR_ADS"
            android:value="true" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="4030500" />
    </application>
</manifest>