我最近成功地制作了横幅广告。 现在我尝试将横幅广告转移到片段中,即使一切似乎都有效:横幅广告也没有显示任何内容。
此处为MainActivity
:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.calculator_toobar);
setSupportActionBar(myToolbar);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.ads_banner) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
BannerAdsFragment firstFragment = new BannerAdsFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction().add(R.id.ads_banner, firstFragment).commit();
}
}
此处为main_activity.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="postfixcalculator.mattiarubini.com.postfixcalculator.MainActivity">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ads_banner"
android:layout_width="match_parent"
android:layout_height="50dp" />
<!--ActionBar-->
<android.support.v7.widget.Toolbar
android:id="@+id/calculator_toobar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#f45342"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<TextView
android:id="@+id/textEquation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="26sp"
android:padding="10sp"
android:layout_weight="1"
android:gravity="center"
android:text="Equation" />
</LinearLayout>
BannerAdsFragment
:
public class BannerAdsFragment extends Fragment {
private static final String TAG = "MainActivity";
private AdView mAdView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.banner_ad_on_top, null);
mAdView = (AdView) rootView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.banner_ad_on_top, container, false);
}
}
banner_ads_on_top.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Banner ad-->
<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_height="50dp"
android:layout_width="match_parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:1)
您正在再次膨胀布局,这会破坏上一个视图。
而不是:
return inflater.inflate(R.layout.banner_ad_on_top, container, false);
使用:
return rootView;