2011-06-04 67 views
1

我想要在屏幕底部顯示固定廣告的項目列表。在列表中包含固定廣告

這是快速和骯髒的代碼,我至今(不包括廣告):

public class SoundBoard extends ListActivity { 
private SoundManager mSoundManager; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

     setListAdapter(new ArrayAdapter<String>(this, R.layout.soundboard_item, CLIPS_STRINGS)); 



     ListView lv = getListView(); 

     mSoundManager = new SoundManager(); 
     mSoundManager.initSounds(getBaseContext()); 
     mSoundManager.addSound(1, R.raw.bad_seasons); 
     mSoundManager.addSound(2, R.raw.friend_at_airport); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // When clicked, show a toast with the TextView text 
      String selection; 
      selection = (String) ((TextView) view).getText(); 
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 


      if (selection == "Bad Seasons") 
       mSoundManager.playSound(1); 
      if (selection == "Friend at Airport") 
       mSoundManager.playSound(2); 

     } 
     }); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if ((keyCode == KeyEvent.KEYCODE_HOME)){ 
     finish(); 
    } 
    if ((keyCode == KeyEvent.KEYCODE_BACK)){ 
     finish(); 
    } 
    return super.onKeyDown(keyCode, event); 
} 

static final String[] CLIPS_STRINGS = new String[] { 
    "Bad Seasons", "Friend at Airport" 
}; 

} 

我可能會使用AdMob SDK。這樣做的最好方法是什麼?

回答

2

添加setContentView(R.layout.soundboard);到您的onCreate方法,然後創建佈局兩個XML文件:

soundboard.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
    <ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"/> 
    <include layout="@layout/ad"/> 
</LinearLayout> 

和ad.xml其中將包含您的佈局廣告。

+0

除了這個答案,你必須添加AdView adView =(AdView)this.findViewById(R.id.adView);和adView.loadAd(新的AdRequest());加載廣告。真棒回答謝謝! – HighLife 2011-06-04 21:27:42

+0

沒問題,只記得標記爲答案:) – khellang 2011-06-04 21:28:28

0

從AdMob SDK創建一個新的AdView,並將新視圖設置爲列表視圖的頁腳。喜歡的東西:

lv.addFooterView(myAdMobView); 
+0

This works but but note that it will inherit the padding and margin of the ListView – fedmich 2014-05-03 07:03:26