Ich bin neu in Android-Programmierung und ich weiß noch nicht viele Dinge. Was ich hier erreichen will, ist so etwas. Wenn der Benutzer keine Internetverbindung hat, werden die Anzeigen nicht angezeigt oder wenn die Anzeigen aus irgendeinem Grund nicht geladen werden, werden die Anzeigen nicht angezeigt. Das Layout bleibt jedoch gleich, dh die Werbefläche bleibt leer. Ich habe die Anzeigenansicht innerhalb eines Relativen Layouts verzerrt und dann eine Funktion erstellt, die prüft, ob die Anzeigen geladen sind oder nicht. Dann ändert sich die Sichtbarkeit des Layouts. Dies scheint zu funktionieren und die Leerraumprobleme zu lösen Belastung. Aber ich denke, dass dies nicht der beste Weg ist, dies zu tun, und es muss einen besseren Weg geben.Constraint Layout Admob Problem
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admobTest.MainActivity"
tools:layout_editor_absoluteY="81dp">
<Button
android:id="@+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/btnExit"
ads:layout_constraintBaseline_toBaselineOf="@+id/btnViewBuildProp"
ads:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnViewBuildProp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/btnViewBuildProp"
ads:layout_constraintBottom_toTopOf="@+id/relativeAdsLayout"
ads:layout_constraintStart_toStartOf="parent" />
/>
<TextView
android:id="@+id/txvStatus"
android:layout_width="368dp"
android:layout_height="0dp"
android:text="TextView"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
<TextView
android:id="@+id/txvDeviceInfo"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
ads:layout_constraintBottom_toTopOf="@+id/btnViewBuildProp"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintTop_toBottomOf="@+id/txvStatus" />
<RelativeLayout
android:id="@+id/relativeAdsLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:visibility="gone"
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintTop_toBottomOf="@+id/btnExit">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
ads:adSize="@string/adSize"
ads:adUnitId="@string/adUnitId"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="457dp" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java nur relevante Bits
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showAds(true); // this displays the ads
}
private void showAds(Boolean doShowAds) {
RelativeLayout relativeAdsLayout = findViewById(R.id.relativeAdsLayout);
if (doShowAds.equals(true)) {
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
relativeAdsLayout.setVisibility(View.VISIBLE);
} else if (doShowAds.equals(false)) {
relativeAdsLayout.setVisibility(View.GONE);
}
} //end showAds
} //end class
Warum die unten Abstimmung? Hinterlasse einen Kommentar, anstatt eine Abstimmung ohne Grund zu hinterlassen. – arifur
Welche Schwächen in Ihrer aktuellen Lösung suchen Sie? Suchen Sie nach einer Admob-API-Methode, um das Problem zu lösen? Randnotiz, warum verwenden Sie 'Boolean' Objekte anstelle von' booleschen' Primitiven? – Jon
Wenn es eine Admob-API dafür gibt, dann ja. Und die Antwort auf Ihre zweite Frage ist, weil ich ein Noob bin. – arifur