Scrollview中动态添加widget不能滚动的为题

在Scrollview中使用了动态添加控件,或者Scrollview中包含了高度不固定的元素,如ListView等列表视图,会导致动态添加的内容超出一屏后无法显示的问题。

 

直接上代码:

1.使用LinearLayout作为根节点:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/ll_top"
        android:layout_width="match_parent"
        android:layout_height="50.0dp"
        android:background="#FF4500" >
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0.0dp"
        android:layout_weight="1.0" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/tv_01"
                android:layout_width="fill_parent"
                android:layout_height="50.0dp" />

            <TextView
                android:id="@+id/tv_02"
                android:layout_width="fill_parent"
                android:layout_height="50.0dp" />
            <ListView 
                android:id="@+id/listview_attachemt"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="50.0dp"
        android:background="#FF4500" >
    </LinearLayout>

</LinearLayout>

    重点就是ScrollView使用了android:layout_height="0.0dp" android:layout_weight="1.0"属性,在绘制时告知自身的高度不固定,这样就保证了给ScrollView动态添加内容的时候其高度可变化,推荐使用此法,构思比较简单。

2.或使用RelativeLayout作为根节点:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/ll_top"
        android:layout_width="match_parent"
        android:layout_height="50.0dp"
        android:layout_alignParentTop="true"
        android:background="#FF4500" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="50.0dp"
        android:layout_alignParentBottom="true"
        android:background="#FF4500" >
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/ll_bottom"
        android:layout_below="@id/ll_top"
        android:background="@color/green" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tv_01"
                android:layout_width="fill_parent"
                android:layout_height="50.0dp" />

            <TextView
                android:id="@+id/tv_02"
                android:layout_width="fill_parent"
                android:layout_height="50.0dp" />

            <ListView
                android:id="@+id/listview_attachemt"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

 重点是ScrollView要同时定义below和above属性,不让ScrollView把top或bottom挤出屏幕之外。

 

 

猜你喜欢

转载自yxwang0615.iteye.com/blog/1847034