我有一个DrawerLayout,我想添加一个AdMob片段。我想在片段的底部添加AdMob,但我不知道我是怎么做的。
我该怎么做?
我正在尝试这个。
DrawerLayout.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dl">
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"
android:gravity="bottom"
>
</com.google.android.gms.ads.AdView>
</FrameLayout>
<ListView
android:id="@+id/lv"
android:layout_width="250dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#e9ba68"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:dividerHeight="1dp"
android:background="#ac453c"
android:layout_gravity="start"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
DrawerLayout片段
public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
private ActionBar ab;
private DrawerLayout dl;
private ListView lv;
private ActionBarDrawerToggle tg;
private List<ItensListView> fragments;
private CharSequence tl; //titulo principal
private CharSequence tlf; //titulo fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_drawerlayout);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
init();
if(savedInstanceState == null){
selectedItem(0);
}
}
private void init(){
//actionbar
onConfigActionBar();
//listview
configItensListView();
lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
lv.setOnItemClickListener(this);
//drawerlayout
dl = (DrawerLayout)findViewById(R.id.dl);
//actionbardrawertoggle
tg = new ActionBarDrawerToggle(this, dl, R.drawable.btmenu, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
dl.setDrawerListener(tg);
tl = tlf = getTitle();
}
/** ativa actionbar e botao home na action bar */
private void onConfigActionBar(){
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
tg.onConfigurationChanged(newConfig);
}
/** necessario */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
tg.syncState();
}
/** necessario */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
FragmentTransaction ft;
Fragment frag;
if(item.getItemId() == R.id.action_chat){
frag = new HelloBubblesActivity();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag,"HelloBubblesActivity");
//ft.addToBackStack("back");
ft.commit();
}
if (tg.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** necessario */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);
return true;
}
/** necessario */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean status = dl.isDrawerOpen(lv);
menu.findItem(R.id.action_settings).setVisible(!status);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
selectedItem(position);
}
/** seleciona o fragment q sera usado */
private void selectedItem(int position){
FragmentTransaction ft;
Fragment frag;
switch(position){
case 0:
//frag = new InicioFrag();
frag = new InicioFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag, "InicioFrag");
ft.commit();
break;
case 1:
frag = new ApresentacaoFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
ft.commit();
break;
case 2:
frag = new ComoJogarFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
//ft.addToBackStack("back");
ft.commit();
break;
case 3:
frag = new PerfilFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
//ft.addToBackStack("back");
ft.commit();
break;
case 4:
frag = new UsuariosRanking();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
//ft.addToBackStack("back");
ft.commit();
break;
case 5:
frag = new ContatoFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
//ft.addToBackStack("back");
ft.commit();
break;
case 6:
clearPrefences();
default:
System.exit(0);
break;
}
lv.setItemChecked(position, true);
setCustomTitle(fragments.get(position).getTexto());
dl.closeDrawer(lv);
}
/** define o titulo da actionbar */
private void setCustomTitle(String title){
SpannableString s = new SpannableString(title);
s.setSpan(new TypefaceSpan(this, BatalhaConfigs.FONT), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ab.setTitle(s);
tl = s;
}
@Override
public void onBackPressed() {
BatalhaSemOponenteFrag bsof = (BatalhaSemOponenteFrag)getSupportFragmentManager().findFragmentByTag("BatalhaSemOponenteFrag");
BatalhaComOponenteFrag bcof = (BatalhaComOponenteFrag)getSupportFragmentManager().findFragmentByTag("BatalhaComOponenteFrag");
InicioFrag ini = (InicioFrag)getSupportFragmentManager().findFragmentByTag("InicioFrag");
HelloBubblesActivity chat = (HelloBubblesActivity)getSupportFragmentManager().findFragmentByTag("HelloBubblesActivity");
if(bsof != null && bsof.isVisible()){
return;
}else if(bcof != null && bcof.isVisible()) {
return;
}else if(chat != null && chat.isVisible()){
Bundle params = new Bundle();
params.putSerializable("infoBatalha", BatalhaConfigs.INFO_BATALHA);
params.putSerializable("listBadgesUsuarioGanhou", (Serializable)BatalhaConfigs.LIST_BADGESGANHA);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment frag = new JogarComOponenteFrag();
frag.setArguments(params);
ft.replace(R.id.fl, frag);
ft.commit();
}else if(ini != null && !ini.isVisible()){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment frag = new InicioFrag();
ft.replace(R.id.fl, frag);
ft.commit();
}else if(ini != null && ini.isVisible()){
//System.exit(0);
closeApp();
}else {
super.onBackPressed();
}
}
@Override
protected void onResumeFragments() {
//Log.i("RESUME: ", "FEZ RESUME");
super.onResumeFragments();
}
/** Configura o List com as informacoes **/
private void configItensListView(){
fragments = new ArrayList<ItensListView>();
//define
ItensListView itens1 = new ItensListView("Iniciar o Jogo", R.drawable.menu_jogo);
ItensListView itens2 = new ItensListView("Apresentação do Jogo", R.drawable.menu_apresentacao);
ItensListView itens3 = new ItensListView("Como Jogar", R.drawable.menu_como_jogar);
ItensListView itens4 = new ItensListView("Perfil", R.drawable.menu_perfil);
ItensListView itens5 = new ItensListView("Ranking", R.drawable.menu_ranking);
ItensListView itens6 = new ItensListView("Contato", R.drawable.menu_contato);
ItensListView itens7 = new ItensListView("Trocar Usuário", R.drawable.exit_session);
ItensListView itens8 = new ItensListView("Sair", R.drawable.logout);
//add
fragments.add(itens1);
fragments.add(itens2);
fragments.add(itens3);
fragments.add(itens4);
fragments.add(itens5);
fragments.add(itens6);
fragments.add(itens7);
fragments.add(itens8);
}
private void closeApp(){
AlertDialog.Builder alert = new AlertDialog.Builder(CustomDrawerLayout.this);
alert.setTitle("Batalha Jurídica");
alert.setMessage("Deseja realmente sair ?");
alert.setCancelable(false);
alert.setNegativeButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
alert.setPositiveButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = alert.create();
dialog.show();
}
/** remove todas as preferencias da session */
private void clearPrefences(){
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPrefs", MODE_PRIVATE);;
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();
startActivity(new Intent(getApplicationContext(), LoginView.class));
}
/** AdMob */
public static class AdMobFrag extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ad, container, false);
}
@Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
AdView mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
}
答案 0 :(得分:1)
尝试这样的事情:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dl">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:wieght="1"/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"
android:gravity="bottom"
>
</com.google.android.gms.ads.AdView>
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="250dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#e9ba68"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:dividerHeight="1dp"
android:background="#ac453c"
android:layout_gravity="start"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
将此代码放入Activity onCreate方法:
public void onCreate(Bundle b) {
super.onCreate(b);
...
...
AdView mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
如果有效,请告诉我。