Android的Scrollview滚动视图控件

1.使用时的注意点?

        1.不用再ScrollView内部使用自带滚动条的控件或布局

        2.ScrollView直接子View只能有一个

        3.ScrollView只支持竖直滑动 如果想要横向滑动 HorizontalScrollView

2.具体案例实现

        前台页面代码

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

    <Button
            android:id="@+id/and_di"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="滚动到底部" />

    <Button
            android:id="@+id/and_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="滚动到顶部" />
</LinearLayout>

<ScrollView
    android:id="@+id/sv"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="顶部"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本2"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本3"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本4"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本5"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本6"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本7"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本8"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本9"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本10"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本2"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本3"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本4"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本5"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本6"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本7"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本8"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本9"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本10"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本2"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本3"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本4"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本5"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本6"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本7"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本8"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本9"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本10"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本2"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本3"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本4"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本5"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本6"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本7"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本8"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本9"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本10"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本2"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本3"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本4"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本5"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本6"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本7"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本8"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="文本9"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="底部"/>
    </LinearLayout>
</ScrollView>

后台Java代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private ScrollView sv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button and_top=findViewById(R.id.and_top);
        Button and_di=findViewById(R.id.and_di);
        sv=findViewById(R.id.sv);

        and_di.setOnClickListener(this);
        and_top.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.and_di){
                //滚动到底部
            sv.fullScroll(ScrollView.FOCUS_DOWN);
        }else if (view.getId()==R.id.and_top){
                //滚动到顶部
            sv.fullScroll(ScrollView.FOCUS_UP);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值