Объявления не отображаются в нижней части экрана с помощью DrawerLayout?

Я пытаюсь разместить рекламу внизу экрана. Мне нужно, чтобы mainContent (контейнер фрагментов) использовал весь экран минус 50dp, где я хочу поместить adsContent. Вот xml:

<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- The main content view -->
    <LinearLayout
        android:id="@+id/mainContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"/>

    <LinearLayout
        android:id="@+id/adsContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1"
        android:layout_above="@id/mainContent">
        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:minHeight="50dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_gravity="center|bottom"
            android:layout_weight="5"
            android:layout_marginBottom="10dp"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/banner_ad_unit_id">  </com.google.android.gms.ads.AdView>
    </LinearLayout>
</LinearLayout>

<!-- The navigation drawer -->
<RelativeLayout
    android:id="@+id/drawerPane"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start">

    <!-- Profile Box -->

    <RelativeLayout
        android:id="@+id/profileBox"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/gray"
        android:padding="8dp">

        <ImageView
            android:id="@+id/avatar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/avatar"
            android:orientation="vertical">

            <TextView
                android:id="@+id/userName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/home"
                android:textColor="#fff"
                android:textSize="12sp"
                android:textStyle="bold" />
        </LinearLayout>
    </RelativeLayout>

    <!-- List of Actions (pages) -->
    <ListView
        android:id="@+id/navList"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_below="@+id/profileBox"
        android:background="#ffffffff"
        android:choiceMode="singleChoice" />
</RelativeLayout>

`

С этим кодом реклама не отображается на экране. Если я задаю mainContent высоту 499 dp, я могу видеть рекламу на своем смартфоне, и она правильно расположена, но на других смартфонах это не так. Может кто-нибудь помочь мне?


person Manuel Minniti    schedule 23.05.2017    source источник


Ответы (1)


1. Используйте RelativeLayout для своей части контента и поместите adsContent и mainContent внутри этого RelativeLayout.

2. Добавьте атрибут android:layout_alignParentBottom="true" к adsContent, чтобы он отображался на bottom экрана.

3. Добавьте атрибут android:layout_above="@id/adsContent" в mainContent, чтобы отобразить макет выше adsContent.

Вот рабочий код:

<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/adsContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_alignParentBottom="true">

            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:minHeight="50dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_gravity="center|bottom"
                android:layout_weight="5"
                ads:adSize="SMART_BANNER"
                ads:adUnitId="@string/banner_ad_unit_id">  </com.google.android.gms.ads.AdView>
        </LinearLayout>

        <!-- The main content view -->
        <LinearLayout
            android:id="@+id/mainContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/adsContent"
            android:orientation="vertical">

        </LinearLayout>
    </RelativeLayout>

    <!-- The navigation drawer -->
    <RelativeLayout
        android:id="@+id/drawerPane"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <!-- Profile Box -->

        <RelativeLayout
            android:id="@+id/profileBox"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/gray"
            android:padding="8dp">

            <ImageView
                android:id="@+id/avatar"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginTop="15dp"
                android:src="@drawable/ic_launcher" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="42dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                android:layout_toRightOf="@+id/avatar"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/userName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/home"
                    android:textColor="#fff"
                    android:textSize="12sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </RelativeLayout>

        <!-- List of Actions (pages) -->
        <ListView
            android:id="@+id/navList"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/profileBox"
            android:background="#ffffffff"
            android:choiceMode="singleChoice" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>
person Ferdous Ahamed    schedule 23.05.2017
comment
Рад это знать :) - person Ferdous Ahamed; 25.05.2017