Позиционирование чего-то внизу, но alignParentBottom=true не имеет никакого эффекта

Я работаю над приложением для Android и делаю макет, который содержит ряд текстов редактирования, а также содержит рекламу, предоставленную admob.

Для рекламы я хочу, чтобы она была прикреплена к нижней части экрана, однако она оставалась плавающей посередине, прямо под нижней частью основного контента.

В рекламе admob я поставил alignParentBottom=true, но, похоже, это не сработало.

Ниже приведена копия XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText android:id="@+id/newDB_txtConnectionName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Database Connection Name"
        android:singleLine="true"
        android:inputType="textNoSuggestions" />
    <EditText android:id="@+id/newDB_txtServer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Server"
        android:singleLine="true"
        android:inputType="textNoSuggestions" />
    <EditText android:id="@+id/newDB_txtUsername"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        android:singleLine="true"
        android:inputType="textNoSuggestions" />
    <EditText android:id="@+id/newDB_txtPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:password="true"
        android:singleLine="true"
        android:inputType="textNoSuggestions" />
    <EditText android:id="@+id/newDB_txtDatabase"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Database"
        android:singleLine="true"
        android:inputType="textNoSuggestions" />
    <EditText android:id="@+id/newDB_txtPort"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxLength="4"
        android:hint="Port"
        android:text="3306"
        android:singleLine="true"
        android:inputType="textNoSuggestions"/>
    <TextView
        android:layout_marginTop="10dp"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal"
        android:text="Leave database blank to connect to any database on this server" />
    <LinearLayout android:id="@+id/buttonContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        style="?android:attr/buttonBarStyle"
        android:layout_alignParentBottom="true">
        <Button android:id="@+id/newDB_btnTest"
            android:layout_width="0dp" 
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Test Connection"
            style="?android:attr/buttonBarButtonStyle" />
        <Button android:id="@+id/newDB_btnCreate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Create"
            android:enabled="false"
            style="?android:attr/buttonBarButtonStyle" />
    </LinearLayout>
        <com.google.android.gms.ads.AdView android:id="@+id/adView"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="center"
             ads:adUnitId="MY_PUBLISHER_ID"
             ads:adSize="BANNER"
             android:layout_alignParentBottom="true"/>
</LinearLayout>
</ScrollView>

Спасибо за любую помощь, которую вы можете предоставить.


person Boardy    schedule 19.01.2014    source источник


Ответы (2)


alignParentBottom недействителен в LinearLayout, используйте RelativeLayout, если вы хотите использовать этот флаг

person spezzino    schedule 19.01.2014
comment
Спасибо, когда я меняю LinearLayout на RelativeLayout, все группируется в верхней части экрана, все перекрывает друг друга в верхней части экрана. - person Boardy; 20.01.2014
comment
Хорошо, вы можете заменить android:layout_alignParentBottom=true на android:layout_gravity=bottom, тогда не нужно менять RelativeLayout - person spezzino; 20.01.2014
comment
Все еще не повезло, все еще плавает прямо под нижней частью основного графического интерфейса, а не внизу экрана - person Boardy; 20.01.2014
comment
Попробуйте добавить android:fillViewport=true в ScrollView и android:layout_weight=1 в buttonContainer LinearLayout. Затем добавьте android:layout_gravity=bottom в AdView (обратите внимание, что он уже находится в центре, замените его на нижний) - person spezzino; 20.01.2014
comment
Объявление находится внизу, однако контейнер с кнопками теперь увеличивается, чтобы заполнить все пространство, поэтому кнопки становятся огромными, а вертикальная разделительная линия контейнера также доходит до нижней части экрана. - person Boardy; 20.01.2014

Если вы хотите использовать LinearLayout, вам нужно указать что-то, что нужно расширить, чтобы заполнить неиспользуемое пространство. Вы делаете это, указывая элемент, который хотите развернуть, layout_weight="1". Например

<?xml version="1.0" encoding="utf-8"?>
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText android:id="@+id/newDB_txtConnectionName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Database Connection Name"
        android:singleLine="true"
        android:inputType="textNoSuggestions" />
    < lots more EditTexts />
    <LinearLayout android:id="@+id/buttonContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        style="?android:attr/buttonBarStyle"
        **android:layout_weight="1"**>
        <Some Buttons/>
    </LinearLayout>
        <com.google.android.gms.ads.AdView android:id="@+id/adView"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="center"
             ads:adUnitId="MY_PUBLISHER_ID"
             ads:adSize="BANNER"
</LinearLayout>
</ScrollView>
person William    schedule 19.01.2014
comment
Спасибо, но к сожалению без разницы - person Boardy; 20.01.2014
comment
Извините, я пропустил тот факт, что у вас есть layout_gravity=center для AdView. Удалите и это. - person William; 20.01.2014
comment
Спасибо, реклама расположена внизу, но поскольку контейнер с кнопками заполняет неиспользуемое пространство, вертикальная линия разделителя кнопок также уходит вниз, что выглядит немного неаккуратно. Я хочу, чтобы контейнер кнопок был просто высотой кнопок - person Boardy; 21.01.2014
comment
Вам нужно что-то, чтобы заполнить это пространство. Если вы не хотите, чтобы какое-либо из ваших существующих представлений заполняло неиспользуемое пространство, я предлагаю вам создать новое представление (пустой TextView или пустой LinearLayout, возможно) и указать ему layout_weight=1 вместо buttonContainer. Предлагаем вам также прочитать developer.android.com/guide/topics/ui/ объявление-layout.html - person William; 22.01.2014