【问题标题】:Set at the bottom of the screen an admob banner after a listview在列表视图之后在屏幕底部设置一个 admob 横幅
【发布时间】:2020-01-09 16:31:55
【问题描述】:

我有一个显示列表视图的 xml 布局。我还想在此列表视图下方显示一个 admob 横幅,并且无论用户滚动列表视图如何,我都希望始终在底部显示它。我有这个 xml 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
              android:id="@+id/home_layout"
              android:orientation="vertical"
              android:layout_height="wrap_content">
    <!-- Put all your application views here, such as buttons, textviews, edittexts and so on -->
    <ListView
    android:id="@+id/lvImages"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"     
    android:dividerHeight="1dp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
            android:orientation="horizontal"
              android:id="@+id/footerLayout"
              android:layout_height="wrap_content"
              android:gravity="bottom"
              android:layout_centerHorizontal="true"
              android:layout_alignParentBottom="true"
              android:layout_alignBottom="@+id/home_layout">

</LinearLayout>

我在 onCreate 方法中创建了 Admob 横幅:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listado_imagenes_subidas_main);

    // Crear adView.
    adView = new AdView(this);
    adView.setAdUnitId("XXXXXXXXXXXXXXXXXXX");
    adView.setAdSize(AdSize.BANNER);

    LinearLayout layout = (LinearLayout)findViewById(R.id.footerLayout);
    layout.addView(adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    // load list application
    listImagenes = (ListView)findViewById(R.id.lvImages);

    // create new adapter
    ImagenesAdapter adapter = new ImagenesAdapter(this, ListadoImagenes());
    // set adapter to list view
    listImagenes.setAdapter(adapter);
}

如何在列表视图下方显示粘性页脚区域?

提前谢谢你。

【问题讨论】:

    标签: android android-listview admob sticky-footer


    【解决方案1】:

    您需要稍微修改一下布局。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
        <LinearLayout
                android:id="@+id/home_layout"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_above="@+id/footerLayout">
            <!-- Put all your application views here, such as buttons, textviews, edittexts and so on -->
            <ListView
                    android:id="@+id/lvImages"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:dividerHeight="1dp"/>
        </LinearLayout>
        <LinearLayout
                android:id="@+id/footerLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="bottom"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                >
        </LinearLayout>
    </RelativeLayout>
    

    【讨论】:

    • 这就是我要说的。有没有办法让广告在页脚中居中?现在它显示在左侧。
    • 居中改变 adView.setAdSize(AdSize.BANNER);到 adView.setAdSize(AdSize.SMART_BANNER);
    【解决方案2】:

    试试这个,在两个子线性布局中调整android:layout_weight="",以获得您喜欢的高度。

    注意在父线性布局中,我将权重和设置为 100,因此子布局的权重为 80 和 20 (80 + 20 = 100)。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="100" >
    
        <LinearLayout
            android:id="@+id/home_layout"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="80"
            android:orientation="vertical" >
    
            <!-- Put all your application views here, such as buttons, textviews, edittexts and so on -->
    
            <ListView
                android:id="@+id/lvImages"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:dividerHeight="1dp" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/footerLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_alignBottom="@+id/home_layout"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_weight="20"
            android:gravity="bottom"
            android:orientation="horizontal" >
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      这样做。

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
      
          <LinearLayout 
              android:id="@+id/footer"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_alignParentBottom="true"/>
      
      
          <ListView
              android:id="@+id/lvImages"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_alignParentTop="true"
              android:layout_above="@id/footer"
              android:dividerHeight="1dp" />
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案4】:

        试试这个:-

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.ViewRecordActivity" >
        
        <ListView
            android:id="@+id/listviewRecords"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/adFragment"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" >
        </ListView>
        
        <fragment
            android:id="@+id/adFragment"
            android:name="com.example.fragments.AdFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
        
        <TextView
            android:id="@+id/txtEmptyListView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:drawableTop="@drawable/ic_person"
            android:text="@string/no_person_found_description"
            android:textSize="12pt" />
        
        </RelativeLayout>
        

        【讨论】:

          【解决方案5】:
          *
          
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:orientation="vertical">
              <LinearLayout android:layout_width="fill_parent"
                            android:id="@+id/home_layout"
                            android:orientation="vertical"
                            android:layout_height="wrap_content">
                  <!-- Put all your application views here, such as buttons, textviews, edittexts and so on -->
                  <ListView
                  android:id="@+id/lvImages"
                  android:layout_width="match_parent"
                  android:layout_height="0dip"    
                  android:layout_weight="1" 
                  android:dividerHeight="1dp" />
          
              <LinearLayout android:layout_width="match_parent"
                          android:orientation="horizontal"
                            android:id="@+id/footerLayout"
                            android:layout_height="wrap_content"
                            android:gravity="bottom"
                           >
          
              </LinearLayout>
          </LinearLayout>
          

          *

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-01-25
            • 1970-01-01
            • 1970-01-01
            • 2014-02-10
            • 1970-01-01
            相关资源
            最近更新 更多