You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中Google Admob测试广告不显示及TextView内容偏移问题咨询

Alright, let's break down your two Android Studio issues and fix them up— I've dealt with both of these headaches before, so here's what worked for me:

1. Fixing Google AdMob Test Ads Not Showing

First, let's look at your AdView setup. There are a few common pitfalls here that are easy to fix:

  • Fix AdView Layout Parameters
    Your AdView has a hardcoded width (410dp) and height (40dp), which conflicts with the FULL_BANNER ad size (standard dimensions: 468x60dp). The AdMob SDK needs control over the view size to render ads properly. Update your AdView to use flexible dimensions:

    <com.google.android.gms.ads.AdView 
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        ads:adSize="FULL_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
    

    Also, the parent LinearLayout holding the AdView doesn't need extra margins that might push it off-screen— keep it simple with wrap_content for height and match_parent for width.

  • Verify AdMob SDK Integration
    Double-check your setup to make sure the SDK is properly integrated:

    1. In your app-level build.gradle, ensure you have the latest AdMob dependency:
      implementation 'com.google.android.gms:play-services-ads:22.6.0'
      
    2. In your AndroidManifest.xml, add the required metadata (use the test app ID for testing):
      <meta-data
          android:name="com.google.android.gms.ads.APPLICATION_ID"
          android:value="ca-app-pub-3940256099942544~3347511713"/>
      
      Also, make sure you have the internet permissions:
      <uses-permission android:name="android.permission.INTERNET"/>
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
      
  • Ensure Proper Ad Loading Code
    In your Itt_Tarihi1 Activity, initialize AdMob and load the ad correctly:

    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout_name);
    
        // Initialize AdMob
        MobileAds.initialize(this, initializationStatus -> {});
    
        // Load ad
        AdView adView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
    }
    
  • Check Testing Environment

    • If using a physical device, make sure it has a stable internet connection and no ad-blocking apps installed.
    • If using an emulator, use the official Android Studio emulator— third-party emulators sometimes have issues with AdMob ads.
2. Fixing TextView Text Offset

Your TextView's text is offset because of conflicting layout parameters. Here's how to fix it:

  • Remove Fixed Width and Adjust Margins
    You've set a fixed width (360dp) plus layout_marginLeft="25dp", which will push the entire TextView to the right, making the centered text look offset on different screen sizes. Instead, use flexible width and horizontal margins to keep the text centered consistently:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="150dp"
        android:layout_marginHorizontal="25dp" <!-- Adds left/right margins equally -->
        android:text="@string/cog_str1"
        android:textAlignment="center"
        android:textSize="17sp"/>
    
  • Verify Text Alignment
    textAlignment="center" works best when the TextView's width is set to match_parent (or fills the available space). If you need the TextView to be a specific width, use android:gravity="center" instead— this centers the text within the TextView's bounds, regardless of the view's size.

Modified Full Layout XML

Here's your complete layout with both fixes applied:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="600dp" 
    android:orientation="vertical" 
    tools:context=".Itt_Tarihi1">

    <Button 
        android:id="@+id/button3" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Button" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="150dp"
        android:layout_marginHorizontal="25dp"
        android:text="@string/cog_str1"
        android:textAlignment="center"
        android:textSize="17sp"/>

    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp">

        <com.google.android.gms.ads.AdView 
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            ads:adSize="FULL_BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>

    </LinearLayout>
</LinearLayout>

内容的提问来源于stack exchange,提问作者yahell35

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 00:07:50