水平滚动视图隐藏了一些子视图

5

安卓4.1.0版本。

我有一个水平滚动视图,在其内部有一个LinearLayout。 在LinearLayout中,我在片段的onResume方法中通过编程方式添加了一些子元素。 当子元素适合于视图时,一切正常。 但是当子元素不适合于视图时,HorizontalView会隐藏左侧的前n个元素,并保留右侧n个元素的自由空间。

我尝试调用computeScroll或requestLayout/forceLayout方法,但都没有帮助。

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

<HorizontalScrollView
    android:id="@+id/productListScroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/productListLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp" />
</HorizontalScrollView>
...
</LinearLayout>

代码片段中的代码:

    public void onResume() {
         super.onResume();
         productsPanel.removeAllViews();
         for (Product product : currentOrder.getProductList()) {
             productsPanel.addView(new ...); //view with calculated height and width
             productsPanel.addView(new ...); //view with fixed width and height = MATCH_PARENT
         }
    }
2个回答

10
<HorizontalScrollView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        <LinearLayout android:id="@+id/top_buttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <!-- Lots of items -->
        </LinearLayout>
    </HorizontalScrollView>

这是一个问题。如果您为内部容器设置了android:layout_gravity="center_horizontal",则水平滚动视图会隐藏其子项。 链接在这里


谢谢。在找到这个答案之前,我浪费了很多时间。 - MinWan
完美运行...但不理解为什么需要android:layout_gravity="center_horizontal" - Rahul Devanavar
非常感谢。在找到这个答案之前,我浪费了很多时间。 - Praveen Kumar Verma

4

通过设置线性布局layout_gravity=leftgravity=center来解决问题。我也将视图创建代码移动到onActivityCreated中,但我认为这并没有帮助解决这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接