5

Android4.1.0。

その中に1つの水平スクロールビューがあります- 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
         }
    }
4

2 に答える 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"内部コンテナに設定すると、横スクロール ビューはその子を非表示にします。リンクはこちら

于 2015-11-05T12:59:39.363 に答える
4

この問題は、線形レイアウトを設定することで解決されましlayout_gravity=leftgravity=center。ビュー作成コードも に移動onActivityCreatedしましたが、この問題の解決には役立たなかったと思います。

于 2012-12-29T12:33:42.147 に答える