2016-11-24 39 views
0

我對Android佈局非常陌生,我試圖在主視圖底部添加一個AdView,並將主視圖向上移動,縮小其高度。我在某處放置了一段代碼,使廣告顯示在底部,但應用程序的高度並未縮小:廣告佔據了應用程序的底部。我必須編程,沒有xmls。Android:在底部添加廣告並縮小其他視圖

這是我的代碼有:

View mainView; // of type SurfaceView 
    adView = new AdView(this); 
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); 
    adView.setAdSize(AdSize.SMART_BANNER); 
    mainLayout = new RelativeLayout(this); 
    mainLayout.addView(mainView); 
    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    mainLayout.addView(adView, adParams); 
    setContentView(mainLayout);  

不多久我展示的AdView:

adView.setVisibility(View.VISIBLE); 
    adView.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build()); 

在此先感謝。

回答

0

我對android也很新,但聽起來像是需要在adView中添加一個佈局對齊標記。這應該推動應用程序並將其添加到底部。我希望這有一點幫助!

0

其簡單:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_weight="1.0"/> 

<LinearLayout 
    android:layout_height="60dp" 
    android:layout_width="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_height="wrap_content" 
     android:text="add your ad here" 
     android:layout_width="wrap_content"/> 

</LinearLayout> 

+0

,我怎麼能做到這一點編程,沒有XML? –

+0

你到底想要做什麼? –

+0

佈局是爲用戶界面製作的,所以如果你必須使用代碼,至少你必須在佈局中顯示它。 https://www.tutorialspoint.com/android/android_user_interface_layouts.htm https://www.tutorialspoint.com/android/android_acitivities.htm –

相關問題