zoukankan      html  css  js  c++  java
  • android学习ScrollView的使用

      ScrollView 的使用相对来讲比较简单,通过包含更多的布局文件,使得上下滑动可以浏览到更多内容。

      关于ScrollView有几个点需要注意的地方

      1,ScrollView的滚动方式

        ScrollView有两种滚动方式,横向的和纵向的,一般横向的用的比较少。ScrollView控件默认就是纵向滚动的,如果需要横向滚动只需要更改标签

        HorizontalScrollView,即可满足要求

      2,ScrollView默认是在滚动的过程中显示滚动条的,所以如果想隐藏滚动条有两种方式:

        1,通过标签设置:android:scrollbars=“none”

        2, 通过代码设置:setHorizontalScrollBarEenable(false);setVertivalScrollBarEnable(false);

      3,ScrollView的常用方法:

        1,getScrollY()-----返回的是滚动条滑动的距离

        2,getMeasureHeight()------返回的是scrollView的总高度,也就是 滚动的距离+屏幕的宽度

        3,getHeight()-------返回的是显示出来的scroll的高度

      4,ScrollTo 和ScrollBy的区别

        1,ScrollTo :从scroll的开始位置作为参考,进行滚动的距离

        2,ScrollBy:从scroll的当前位置作为参考,进行滚动的距离

    public class MainActivity extends Activity implements View.OnClickListener{
    
    
        private TextView textView;
        private ScrollView scrollView;
        private Button up;
        private Button down;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView = (TextView)findViewById(R.id.textView);
            scrollView = (ScrollView)findViewById(R.id.scroll);
            textView.setText(R.string.text);
            up = (Button)findViewById(R.id.button);
            down = (Button)findViewById(R.id.button2);
            scrollView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch(event.getAction()){
    
                        case MotionEvent.ACTION_MOVE:{
                            if(scrollView.getScaleY()<=0){
                                android.util.Log.i("main","top");
                            }
                            if(scrollView.getMeasuredHeight() == scrollView.getHeight()+scrollView.getScaleY()){
    
                                android.util.Log.i("main","bottom");
                            }
                            break;
                        }
                    }
                    return false;
                }
            });
        }
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.button:{
                    scrollView.scrollBy(0, -30);
                    break;
                }
                case R.id.button2:{
                    scrollView.scrollBy(0,30);
                    break;
                }
            }
        }
    }

       ScrollView布局文件如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="top"
                android:id="@+id/button" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="down"
                android:id="@+id/button2" />
        </LinearLayout>
        <ScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">
            <TextView
                android:id="@+id/textView"
                android:text="@string/hello_world" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </ScrollView>
    </LinearLayout>
  • 相关阅读:
    2407: C语言习题 整数转换成字符串
    2484: 字母图形
    1658: Easier Done Than Said?
    Easier Done Than Said?(应用到的知识)
    1653: Champion of the Swordsmanship
    1614: 五位以内的对称素数
    1612: 最小公倍数
    $ vs $()
    数学
    计算机科学与技术课程
  • 原文地址:https://www.cnblogs.com/techdreaming/p/4998962.html
Copyright © 2011-2022 走看看