浅谈 ScrollView 用法

ScrollView 室友FrameLayout 派生的,他的主要作用是为普通组件添加滚动条。让其可以显示更多的内容。ScrollView里面只能包括一个控件,如果想要包括多个则需要在ScrollView里面添加其他布局例如linearlayouy  在其里面进行布局。 

ScrollView表示是垂直滚动条,HorizontalScrollView表示是水平滚动条。

HorizontalScrollView 和 ScrollView都可以作为最外层布局 。

<?xml version="1.0" encoding="utf-8"?>
<strong><HorizontalScrollView</strong> xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
          <!-- 所需控件 -->
    </LinearLayout>
    

<strong></HorizontalScrollView></strong>
<?xml version="1.0" encoding="utf-8"?>
<<strong>ScrollView</strong> xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <!-- 所需控件 -->
    </LinearLayout>

<<strong>/ScrollView></strong>
也可以嵌套在其他布局里面如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            
              <!-- 所需控件 -->
            
        </LinearLayout>
    </ScrollView>

</LinearLayout>
ScrollView里面不能放listview   gridview 控件,否则则会显示有问题,只会显示listview 和gridview的第一行数据。

如 当我把listview gridview放在ScrollView 外面时候

<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" >

   <strong> <GridView
        android:id="@+id/gd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3"
        android:verticalSpacing="5dp" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/books" />
    <ScrollView</strong>
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


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


    <EditText
        android:id="@+id/ed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@drawable/my_view" />

    <EditText
        android:id="@+id/ed1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@drawable/my_view" />

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progressDrawable="@drawable/my_bar" />
  <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
        android:src="@drawable/logo" />
    
  
    </LinearLayout>
    </ScrollView>

</LinearLayout>
运行显示效果为:



当我把listview gridview放在ScrollView 里面时候

<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" >

   <strong> <ScrollView</strong>
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

           <strong> <GridView
                android:id="@+id/gd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:numColumns="3"
                android:verticalSpacing="5dp" />

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:entries="@array/books" /></strong>

            <EditText
                android:id="@+id/ed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@drawable/my_view" />

            <EditText
                android:id="@+id/ed1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@drawable/my_view" />

            <SeekBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:progressDrawable="@drawable/my_bar" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/logo" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>
运行的效果如下:


都只显示第一行数据。

而且在布局的时候GridView  和ListView 下面会有黄色波浪线     提示:

The vertically scrolling ScrollView should not contain another vertically scrolling widget  (GridView)意思是垂直滚动条不能包含另外的垂直滚动条(GridView


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值