Android滚动视图-确保它始终大于屏幕大小



我有一个滚动视图,里面有多个不同的视图。我想要的是让滚动视图中除了最后一个元素之外的所有元素都填满整个屏幕。然后,用户可以向下滚动以显示滚动视图中的最后一个项目。我不希望最后一项在任何其他情况下都可见。

我的XML如下所示:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="3.45"
    android:fillViewport="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <View 1 ... android:weight="2.25" />
        <View 2 ... android:weight="0.8" />
        <View 3 ... android:weight="0.40" /> //last item
    </LinearLayout>
</ScrollView>

这目前所做的是完全填充整个屏幕,但没有最后一项推送。

根据最后一篇文章(现已删除),您应该将最后一项放置在LinearLayout组之外或其他LinearLayout中。它将像一样

<ScrollView
    android:layout_width="match_parent"<!-- added-->
    android:layout_height="match_parent"><!-- added-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:oriention="vertical"><!-- added-->
        <LinearLayout
            android:id="@+id/visible_items"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"><!-- changed-->
            ...{items}
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            {last item}
        </LinearLayout>
    </LinearLayout>
<ScrollView>  

更新:现在你需要扩展linearLayout的高度来填充屏幕:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
final Point point = new Point();
    try {
        display.getSize(point);
    } catch (java.lang.NoSuchMethodError ignore) { // Older device
        point.x = display.getWidth();
        point.y = display.getHeight();
    }
LinearLayout layout = (LinearLayout) findViewById(R.id.visible_items);
LayoutParams lp = layout.getLayoutParams();
lp.height = point.y;
layout.setLayoutParams(lp);  

注意:将以上代码放在onWindowFocusChanged()活动方法

最新更新