隐藏一些孩子的水平滚动视图

ily*_*olk 4 android horizontalscrollview

Android 4.1.0.

我有一个水平滚动视图 - 里面 - LinearLayout.在LinearLayout中,我以编程方式在片段中添加一些子项onResume.如果儿童适合视野,一切都很好.如果儿童不适合视图,则从左侧HorizontalView隐藏前n个元素,并在右侧保留n个元素的可用空间.

我试着打电话computeScrollrequestLayout/ 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>
Run Code Online (Sandbox Code Playgroud)

和片段中的代码:

    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
         }
    }
Run Code Online (Sandbox Code Playgroud)

Mee*_*axi 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>
Run Code Online (Sandbox Code Playgroud)

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


ily*_*olk 4

通过设置线性布局解决了这个问题layout_gravity=leftgravity=center我还将视图创建代码移至onActivityCreated,但我认为这对解决这个问题没有帮助。