的LinearLayout覆盖整个屏幕? [英] LinearLayout covering entire screen?
问题描述
我最近做了一个游戏LibGDX,我添加了一个的LinearLayout
在 MainActivity.java
让我可以放置AdMob广告的广告出现。一切都运行良好和花花公子,但问题是布局覆盖有黑色的整个屏幕,我无法加载或玩游戏。有没有一种方法,使屏幕(大部分广告)顶部的LinearLayout权利,有它存在,同时又能使游戏加载和播放?因为现在我的应用程序仅仅是一个黑色的屏幕与广告在顶部。
下面是MainActivity.java
包com.me.mygdxgame;
进口android.os.Bundle;
进口android.widget.LinearLayout;
进口com.badlogic.gdx.backends.android.AndroidApplication;
进口com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
进口com.google.android.gms.ads.AdRequest;
进口com.google.android.gms.ads.AdSize;
进口com.google.android.gms.ads.AdView;
公共类MainActivity扩展AndroidApplication {
AD浏览报AD浏览报;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration CFG =新AndroidApplicationConfiguration();
cfg.useGL20 = TRUE;
初始化(新MyGame(),CFG);
AD浏览报=新的AD浏览报(本);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(我的广告单元ID);
的setContentView(R.layout.main);
的LinearLayout布局=(的LinearLayout)findViewById(R.id.linearLayout);
layout.addView(AD浏览报);
如果(布局== NULL)
的System.out.println(布局不工作);
如果(布局!= NULL)
的System.out.println(布局工程);
如果(AD浏览报== NULL)
的System.out.println(AD浏览报不工作);
如果(AD浏览报!= NULL)
的System.out.println(AD浏览报作品);
AdRequest adRequest =新AdRequest.Builder()
.addTestDevice(我的设备ID)
。建立();
如果(adRequest == NULL)
的System.out.println(adRequest不工作);
如果(adRequest!= NULL)
的System.out.println(adRequest工程);
adView.loadAd(adRequest);
的setContentView(布局);
}
公共无效的onDestroy(){
如果(AD浏览报!= NULL){
adView.destroy();
}
super.onDestroy();
}
}
下面是main.xml中:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / LinearLayout中
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
< / LinearLayout中>
对于线性布局你给了:
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
这是导致它填满整个屏幕。你可以试试这个:
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
您也可以使用一些DP值高度或宽度指定特定的高度
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>
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
这篇关于的LinearLayout覆盖整个屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!