【问题标题】:Extra space in horizontalscrollview after orientation changes方向更改后水平滚动视图中的额外空间
【发布时间】:2017-04-26 21:34:30
【问题描述】:

在我的布局中,我必须在纵向模式下显示三行,在横向模式下显示 2 行。我必须在运行时在这些行中添加多个图像视图。我的这些行在 Horizo​​ntalScrollView 中。当方向改变时,我从行中删除所有视图,设置行数(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>

我认为问题在于当有两行(横向模式)时,行的宽度更大,而在纵向模式下,内容分为三行,前两行宽度保持不变。尽管如此,我要从行中删除所有视图,然后添加新视图。应该根据内容减小行宽。布局也在片段内,我在活动清单android:configChanges="orientation|keyboardHidden|screenSize" 中添加了以下内容以防止片段重新生成。 知道我哪里出错了吗?

【问题讨论】:

    标签: android horizontalscrollview orientation-changes


    【解决方案1】:

    解决方案:

    <HorizontalScrollView
        ......
        android:fillViewport="true"  >
    

    【讨论】:

    • 请不要使用链接做广告!
    【解决方案2】:

    添加

    android:fillViewport="true"
    

    Horizo​​ntalScrollView 中的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多