UI控件之ScrollView(滚动条)

(一)概述
ScrollView控件只是支持垂直滚动,而且在ScrollView中只能包含一个控件,通常是在< ScrollView >标签中定义了一个<LinearLayout>标签并且在<LinearLayout>标签中android:orientation属性值设置为vertical,然后在<LinearLayout>标签中放置多个控件,如果<LinearLayout>标签中的控件所占用的总高度超出屏幕的高度,就会在屏幕的右侧出现一个滚动条。
ScrollView的类结构如下:
这里写图片描述
这里写图片描述

(二)实际开发中的应用
这里写图片描述 ,我们放到listView中进行详细的讲解;
No.1 —-滚动到底部
这里写图片描述

运行效果:
这里写图片描述
实现代码:
xml文件

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scroview1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android_scrollview.MainActivity" >

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

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="滚到底部" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="滚到顶部" />
        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</ScrollView>

java代码:

public class MainActivity2 extends Activity implements OnClickListener {

    private Button btn_down ,btn_up;
    private ScrollView scrollView;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        bindView();
    }

    private void bindView(){
        btn_down = (Button) findViewById(R.id.btn1);
        btn_up = (Button) findViewById(R.id.btn2);
        scrollView = (ScrollView) findViewById(R.id.scroview1);
        textView = (TextView) findViewById(R.id.tv1);

        btn_down.setOnClickListener(this);
        btn_up.setOnClickListener(this);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 50; i++) {
            sb.append("哇哈哈~~~"+ i + "\n");
        }
        textView.setText(sb.toString());

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.btn1:
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            break;
        case R.id.btn2:
            scrollView.fullScroll(ScrollView.FOCUS_UP);
            break;
        }
    }
}

这里写图片描述
这里写图片描述

    });
}

这里写图片描述

(三)设置的滚动的滑块图片
这里写图片描述
(四)隐藏滑块
这里写图片描述

(五)设置滚动速度
这里写图片描述
这里写图片描述

如果对你有启发或有用就帮我点个赞吧~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值