安卓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
}
}
android:layout_gravity="center_horizontal"
。 - Rahul Devanavar