2014-06-27 117 views
0

我已經在xcode中開發了一款遊戲,使用cocos2d-x &轉換爲android.Then我添加谷歌播放服務廣告,但它顯示在leftsidecorner。我想在中間顯示它。 請任何人都幫我找到解決方案。谷歌播放服務廣告顯示在角落中android

private Point getDisplaySize(Display d) 
{ 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
    { 
     return getDisplaySizeGE11(d); 
    } 
    return getDisplaySizeLT11(d); 
} 
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 
private Point getDisplaySizeGE11(Display d) 
{ 
    Point p = new Point(0, 1); 
    d.getSize(p); 
    return p; 
} 
private Point getDisplaySizeLT11(Display d) 
{ 
    try 
    { 
    Method getWidth = Display.class.getMethod("getWidth", new Class[] {}); 
    Method getHeight = Display.class.getMethod("getHeight", new Class[] {}); 
    return new Point(((Integer) getWidth.invoke(d,(Object[]) null)).intValue(),        
    ((Integer) getHeight.invoke(d, (Object[]) null)).intValue()); 
    } 
    catch (NoSuchMethodException e2) // None of these exceptions should ever occur. 
    { 
     return new Point(-1, -1); 
    } 
    catch (IllegalArgumentException e2) 
    { 
     return new Point(-2, -2); 
    } 
    catch (IllegalAccessException e2) 
    { 
     return new Point(-3, -3); 
    } 
    catch (InvocationTargetException e2) 
    { 
     return new Point(-4, -4); 
    } 
    } 
     @Override 

protected void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    int width = getDisplaySize(getWindowManager().getDefaultDisplay()).y; 
    FrameLayout.LayoutParams adParams = new FrameLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, 
    LayoutParams.WRAP_CONTENT); 
    adParams.gravity = Gravity.BOTTOM; 
    adView = new AdView(this); 
    adView.setAdSize(AdSize.BANNER); 
    adView.setAdUnitId(AD_UNIT_ID); 


    AdRequest adRequest = new AdRequest.Builder() 
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
    .addTestDevice("HASH_DEVICE_ID") 
    .build(); 
    adView.loadAd(adRequest); 
    adView.setBackgroundColor(Color.BLACK); 
    adView.setBackgroundColor(0); 
    addContentView(adView,adParams); 
    _appActiviy = this; 
    } 
+0

發佈您的佈局。 –

+0

我沒有使用任何佈局。我只使用.java文件中的Google Play服務廣告代碼。 – arunkumar

+0

如果您正在顯示任何內容,請使用佈局。 –

回答

0

在上面的代碼中進行以下更改。

找到下面的代碼

adParams.gravity = Gravity.BOTTOM; 

其更改如下

adParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; 

然後附加在您的應用底部的中間顯示。

+0

感謝您的評論。工作正常。 – arunkumar