问题
I recently made a game with LibGDX, and I added a LinearLayout
in the MainActivity.java
so that I could place an AdMob ad there. Everything is running fine and dandy, but the problem is the layout covers the entire screen with black, and I'm not able to load or play the game. Is there a way to make the LinearLayout right at the top of the screen (where most ads are), and have it exist there while allowing the game to loaded and be played? Because right now my app is just a black screen with an ad at the top.
Here is the MainActivity.java
package com.me.mygdxgame;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AndroidApplication {
AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
initialize(new MyGame(), cfg);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("My Ad Unit ID");
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
layout.addView(adView);
if (layout ==null)
System.out.println("layout doesn't work");
if (layout !=null)
System.out.println("layout works");
if (adView == null)
System.out.println("adView doesn't work");
if (adView != null)
System.out.println("adView works");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("My Device ID")
.build();
if (adRequest == null)
System.out.println("adRequest doesn't work");
if (adRequest != null)
System.out.println("adRequest works");
adView.loadAd(adRequest);
setContentView(layout);
}
public void onDestroy(){
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
Here's the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
回答1:
For linear layout you have given :
android:layout_width="fill_parent"
android:layout_height="fill_parent"
This is causing it to fill the entire screen. You can try this :
android:layout_width="fill_parent"
android:layout_height="wrap_content"
You can also specify specific height using some dp value for height or width
回答2:
Your problem is that libGDX AndroidApplication#initialize (which you call in #onCreate) calls setContentView and then you replace that twice. #setContentView should only be called once with the layout for your Activity.
来源:https://stackoverflow.com/questions/22446713/linearlayout-covering-entire-screen