在列表视图后面的屏幕底部设置一个admob横幅

时间:2014-10-15 10:33:41

标签: android android-listview admob sticky-footer

我有一个显示列表视图的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);
}

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

提前谢谢。

5 个答案:

答案 0 :(得分: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/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>

答案 1 :(得分:1)

尝试此操作,调整子线性布局中的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>

答案 2 :(得分:0)

这样做。

<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>

答案 3 :(得分:0)

试试这个: -

<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>

答案 4 :(得分:0)

*

<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>

*