LinearLayout не работает, если он установлен как родительский для ScrollView - PullRequest
0 голосов
/ 09 мая 2020

Я хочу сделать так, чтобы LinearLayout отображал рекламу внизу экрана. Это баннерная реклама. Я пробовал много комбинаций, и когда я помещаю LinearLayout над Scrollview в макете. xml иерархия, я получаю сообщение об ошибке типа «ScrollView может содержать только один прямой дочерний элемент», и приложение вылетает.

Я могу заставить приложение работать, поместив ScrollView над LinearLayout объявления, но в этом случае объявление появляется под всеми кнопками, тогда как оно должно отображаться в иерархии над кнопками прокрутки в нижней части экрана.

Как мне сделать так, чтобы баннер появлялся внизу экрана с помощью кнопок прокрутки выше?

Это отрывок из моего файла main. xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml   Era LinearLayout-->

<RelativeLayout 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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!--  
        <com.facebook.ads.AdView
            android:id="@id/banner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
             />
    -->


      <ScrollView
          android:id="@+id/scroll"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content">

       <LinearLayout
           android:id="@+id/banner_container"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true"
           android:orientation="vertical"
           app:layout_constraintBottom_toBottomOf="parent">

          <Button
              android:id="@+id/holaPlayerBtn"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/custom_button_yellow"
              android:text="@string/hola"
              android:textColor="#000000"
              android:textStyle="bold" />

          <Button
              android:id="@+id/adiosPlayerBtn"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/custom_button_yellow"
              android:text="@string/adios"
              android:textColor="#000000"
              android:textStyle="bold" />


          ... many other buttons here ...

          <Button
              android:id="@+id/hastaprontoPlayerBtn"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/custom_button_yellow"
              android:text="@string/hasta_pronto"
              android:textColor="#000000"
              android:textStyle="bold" />

       </LinearLayout>
   </ScrollView>

</RelativeLayout>

Ответы [ 3 ]

0 голосов
/ 09 мая 2020

Вот решение:

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml   Era LinearLayout-->

<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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/banner_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:id="@+id/holaPlayerBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/holo_blue_dark"
                android:text="hola"
                android:textColor="#000000"
                android:textStyle="bold" />

            <Button
                android:id="@+id/adiosPlayerBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/holo_green_dark"
                android:text="adios"
                android:textColor="#000000"
                android:textStyle="bold" />

            // Add more buttons/views as your wish.

        </LinearLayout>
    </ScrollView>

    // This will always remain at bottom.
    <Button
        android:id="@+id/hastaprontoPlayerBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        android:text="hasta_pronto"
        android:textColor="#000000"
        android:textStyle="bold" />

</LinearLayout>
0 голосов
/ 09 мая 2020

Мне удалось заставить его работать так, как ожидалось: выигрышная основная. xml это:

<?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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

       <ScrollView
          android:id="@+id/scroll"
          android:layout_width="match_parent"
          android:layout_height="0dp"
           android:layout_weight="1">

         <LinearLayout
             android:id="@+id/button_container"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="vertical">

              <Button
              android:id="@+id/holaPlayerBtn"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/custom_button_yellow"
              android:text="@string/hola"
              android:textColor="#000000"
              android:textStyle="bold" />

          <Button
              android:id="@+id/adiosPlayerBtn"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/custom_button_yellow"
              android:text="@string/adios"
              android:textColor="#000000"
              android:textStyle="bold" />

 <!-- Lots of buttons are placed here -->

          <Button
              android:id="@+id/hastaprontoPlayerBtn"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/custom_button_yellow"
              android:text="@string/hasta_pronto"
              android:textColor="#000000"
              android:textStyle="bold" />

              </LinearLayout>
       </ScrollView>
  <LinearLayout
      android:id="@+id/banner_container"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
  </LinearLayout>
</LinearLayout>
0 голосов
/ 09 мая 2020

Вам необходимо установить fillViewport:

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" <!-- Here --> >
    ...

 </ScrollView>

Что такое fillViewPort? Ссылки из здесь android: fillViewport. Определяет, должно ли scrollview растягивать свое содержимое для заполнения области просмотра.

...