Дополнительное пространство в горизонтальной прокрутке после изменения ориентации

В моем макете я должен показать три ряда в портретном режиме и 2 в ландшафтном режиме. Мне нужно добавить несколько изображений в эти строки во время выполнения. Мои эти строки находятся в HorizontalScrollView. Когда ориентация меняется, я удаляю все виды из строк, устанавливаю количество строк (2 или 3), а затем снова добавляю изображения в видимые строки. Предположим, я сначала был в портретном режиме, я поворачиваю устройство в альбомный режим, тогда содержимое размещается правильно. Но когда я снова беру портретный режим устройства, в конце строк остается пустое дополнительное пространство. Мой макет xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

<HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginTop="15dp"
        android:tag="scrollView"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:weightSum="3" >

            <LinearLayout
                android:id="@+id/shelf1_container"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="15dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1" >

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

                    <ImageView
                        android:id="@+id/shelf1_base"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/shelf_bg" />

                    <LinearLayout
                        android:id="@+id/shelf1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="15dp"
                        android:gravity="bottom"
                        android:orientation="horizontal"
                        android:tag="shelf1" >
                    </LinearLayout>
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/shelf2_container"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="15dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1" >

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

                    <ImageView
                        android:id="@+id/shelf2_base"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/shelf_bg" />

                    <LinearLayout
                        android:id="@+id/shelf2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="15dp"
                        android:gravity="bottom"
                        android:orientation="horizontal"
                        android:tag="shelf1" >
                    </LinearLayout>
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/shelf3_container"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="15dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1" >

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

                    <ImageView
                        android:id="@+id/shelf3_base"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/shelf_bg" />

                    <LinearLayout
                        android:id="@+id/shelf3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="15dp"
                        android:gravity="bottom"
                        android:orientation="horizontal"
                        android:tag="shelf1" >
                    </LinearLayout>
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

Я думаю, проблема в том, что когда есть две строки (альбомный режим), строки имеют большую ширину, а когда дело доходит до портретного режима, содержимое делится на три строки, ширина первых 2 строк остается одинаковой. Несмотря на это, я удаляю все представления из строк, а затем добавляю новые. Ширина строк должна быть уменьшена в соответствии с содержимым. Кроме того, макет находится внутри фрагмента, и я добавил следующее в манифест активности android:configChanges="orientation|keyboardHidden|screenSize", чтобы предотвратить воссоздание фрагмента. Есть идеи, где я ошибаюсь?


person Khawar Raza    schedule 12.09.2013    source источник


Ответы (2)


Решение :

<HorizontalScrollView
    ......
    android:fillViewport="true"  >
person Miraj    schedule 26.04.2017
comment
Пожалуйста, не используйте ссылки для рекламы! - person Failed Scientist; 26.04.2017

Добавлять

android:fillViewport="true"

свойство в вашем HorizontalScrollView.

person Qandil Tariq    schedule 26.04.2017