2016-08-24 108 views
0

我是新來的android開發,我很困惑如何將admob橫幅添加到片段活動(我認爲它的調用片段)。我正在修改源代碼。如何在片段視圖中添加admob橫幅?

我有一個佈局文件名爲fragment_ftp.xml及其如下

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:ads="http://schemas.android.com/apk/res-auto"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="" 
     android:id="@+id/statusText" 
     android:layout_above="@+id/startStopButton" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="99dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="" 
     android:id="@+id/ftpAddressText" 
     android:layout_below="@+id/startStopButton" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/start_ftp" 
     android:id="@+id/startStopButton" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/primary_red" 
     android:text="" 
     android:id="@+id/warningText" 
     android:layout_above="@+id/startStopButton" 
     android:layout_centerHorizontal="true" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/ftp_image" 
     android:layout_above="@+id/warningText" 
     android:layout_centerHorizontal="true" /> 
    <RelativeLayout 
     android:id="@+id/relativeLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true"> 

     <com.google.android.gms.ads.AdView 
      android:id="@+id/adView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      ads:adSize="SMART_BANNER" 
      ads:adUnitId="@string/banner_home_footer" /> 
    </RelativeLayout> 

</RelativeLayout> 

我已經添加了AdMob的旗幟,在預覽中,我可以看到。請檢查下面的截圖。 enter image description here

但問題是,我應該在哪裏編寫這個admob橫幅的java代碼。我沒有這個fragment_ftp.xml的相應的活動。

搜索fragment_ftp,我只能看到如下的java文件。 FTPServerFragment.java

package com.filename.fragments; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.SharedPreferences; 
import android.net.ConnectivityManager; 


public class FTPServerFragment extends Fragment { 

    TextView statusText,warningText,ftpAddrText; 
    Button ftpBtn; 
    Futils utils = new Futils(); 
    private MainActivity mainActivity; 
    private View rootView; 
    private BroadcastReceiver mWifiReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo netInfo = conMan.getActiveNetworkInfo(); 
      if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI){ 
       warningText.setText(""); 
      } 
      else{ 
       stopServer(); 
       statusText.setText(utils.getString(getContext(),R.string.ftp_status_not_running)); 
       warningText.setText(utils.getString(getContext(),R.string.ftp_no_wifi)); 
       ftpAddrText.setText(""); 
       ftpBtn.setText(utils.getString(getContext(),R.string.start_ftp)); 
      } 
     } 
    }; 
    private BroadcastReceiver ftpReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if(action == FTPService.ACTION_STARTED) { 
       statusText.setText(utils.getString(getContext(), R.string.ftp_status_running)); 
       warningText.setText(""); 
       ftpAddrText.setText(getFTPAddressString()); 
       ftpBtn.setText(utils.getString(getContext(),R.string.stop_ftp)); 
      } 
      else if(action == FTPService.ACTION_FAILEDTOSTART){ 
       statusText.setText(utils.getString(getContext(),R.string.ftp_status_not_running)); 
       warningText.setText("Oops! Something went wrong"); 
       ftpAddrText.setText(""); 
       ftpBtn.setText(utils.getString(getContext(),R.string.start_ftp)); 
      } 
      else if(action == FTPService.ACTION_STOPPED){ 
       statusText.setText(utils.getString(getContext(),R.string.ftp_status_not_running)); 
       ftpAddrText.setText(""); 
       ftpBtn.setText(utils.getString(getContext(),R.string.start_ftp)); 
      } 
     } 
    }; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setHasOptionsMenu(false); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     rootView = inflater.inflate(R.layout.fragment_ftp,container,false); 
//  return inflater.inflate(R.layout.article_view, container, false); 
     statusText =(TextView) rootView.findViewById(R.id.statusText); 
     warningText = (TextView) rootView.findViewById(R.id.warningText); 
     ftpAddrText = (TextView) rootView.findViewById(R.id.ftpAddressText); 
     ftpBtn = (Button) rootView.findViewById(R.id.startStopButton); 

     SharedPreferences Sp = PreferenceManager.getDefaultSharedPreferences(getContext()); 
     int th = Integer.parseInt(Sp.getString("theme", "0")); 
     // checking if theme should be set light/dark or automatic 
     int theme1 = th == 2 ? PreferenceUtils.hourOfDay() : th; 
     ImageView ftpImage = (ImageView)rootView.findViewById(R.id.ftp_image); 

     //light theme 
     if(theme1 == 0){ 
      ftpImage.setImageResource(R.drawable.ic_ftp_light); 
     }else{ 
      //dark 
      ftpImage.setImageResource(R.drawable.ic_ftp_dark); 
     } 
     ftpBtn.setOnClickListener(new View.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       if(!FTPService.isRunning()){ 
        if(FTPService.isConnectedToWifi(getContext())) 
         startServer(); 
        else 
         warningText.setText(utils.getString(getContext(),R.string.ftp_no_wifi)); 
       } 
       else{ 
        stopServer(); 
       } 
      } 
     }); 

     return rootView; 
    } 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setRetainInstance(true); 
     mainActivity=(MainActivity)getActivity(); 
     mainActivity.setActionBarTitle(utils.getString(getActivity(), R.string.ftp)); 
     mainActivity.floatingActionButton.hideMenuButton(true); 
     mainActivity.buttonBarFrame.setVisibility(View.GONE); 
     mainActivity.supportInvalidateOptionsMenu(); 
    } 
    @Override 
    public void onDestroy(){ 
     super.onDestroy(); 
    } 

    private void startServer() { 
     getContext().sendBroadcast(new Intent(FTPService.ACTION_START_FTPSERVER)); 
    } 

    private void stopServer() { 
     getContext().sendBroadcast(new Intent(FTPService.ACTION_STOP_FTPSERVER)); 
    } 

    @Override 
    public void onResume(){ 
     super.onResume(); 
     updateStatus(); 
     IntentFilter wifiFilter = new IntentFilter(); 
     wifiFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 
     getContext().registerReceiver(mWifiReceiver,wifiFilter); 
     IntentFilter ftpFilter = new IntentFilter(); 
     ftpFilter.addAction(FTPService.ACTION_STARTED); 
     ftpFilter.addAction(FTPService.ACTION_STOPPED); 
     ftpFilter.addAction(FTPService.ACTION_FAILEDTOSTART); 
     getContext().registerReceiver(ftpReceiver,ftpFilter); 
    } 

    @Override 
    public void onPause(){ 
     super.onPause(); 
     getContext().unregisterReceiver(mWifiReceiver); 
     getContext().unregisterReceiver(ftpReceiver); 
    } 
    private void updateStatus(){ 
     if(FTPService.isRunning()){ 
      statusText.setText(utils.getString(getContext(),R.string.ftp_status_running)); 
      ftpBtn.setText(utils.getString(getContext(),R.string.stop_ftp)); 
      ftpAddrText.setText(getFTPAddressString()); 
     } 
     else{ 
      statusText.setText(utils.getString(getContext(),R.string.ftp_status_not_running)); 
      ftpBtn.setText(utils.getString(getContext(),R.string.start_ftp)); 
     } 
    } 
    private String getFTPAddressString(){ 
     return "ftp://"+FTPService.getLocalInetAddress(getContext()).getHostAddress()+":"+FTPService.getPort(); 
    } 
} 

我這個java的頁面,在這裏我將寫AdMob的電話嗎?

+0

你什麼意思,你沒有相應的活動?您可以在活動中使用該佈局,或者您需要將片段加載到活動中。 –

+0

嗨,我只能在上面的java文件中看到fragment_ftp佈局。但我無法在該Java文件中添加admob代碼。 –

+0

我沒有看到任何與該文件中的廣告相關的代碼,所以你必須更清楚你爲什麼不能添加它 –

回答