在列表中包含固定广告

问题描述:

我想要在屏幕底部显示固定广告的项目列表。在列表中包含固定广告

这是快速和肮脏的代码,我至今(不包括广告):

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。这样做的最好方法是什么?

添加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

从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

如果您正在寻找能够解决您问题的示例布局,请参阅Scrolling Text Above Buttons, Buttons Fixed At Bottom