ScrollView的使用以及简单Demo
2015-01-31 17:24
218 查看
ScrollView 就是我们常见的垂直滚动条
HorizontalScrollView是我们常见的水平滚动条
什么时候会用到他呢?
当Activity中显示不下那么多内容时,就可以在布局中加入这两个组件,用这两个组件包含显示在Actvity中的内容
可以给它绑定监听器scroll.setOnTouchListener(new OnTouchListener()....可以监听滚动的动作
而且可以通过代码实现滚动,如下面将给出的up和down两个按钮就可以实现上下滚动
具体代码以及实现效果如下:
MainActivity.java
HorizontalScrollView是我们常见的水平滚动条
什么时候会用到他呢?
当Activity中显示不下那么多内容时,就可以在布局中加入这两个组件,用这两个组件包含显示在Actvity中的内容
可以给它绑定监听器scroll.setOnTouchListener(new OnTouchListener()....可以监听滚动的动作
而且可以通过代码实现滚动,如下面将给出的up和down两个按钮就可以实现上下滚动
具体代码以及实现效果如下:
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{ private TextView textView; private TextView textMark; private ScrollView scroll; private Button upBtn,downBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textMark = (TextView) findViewById(R.id.mark); textView = (TextView) findViewById(R.id.tv); textView.setText(getResources().getString(R.string.content)); upBtn = (Button)findViewById(R.id.up); upBtn.setOnClickListener(this); downBtn = (Button)findViewById(R.id.down); downBtn.setOnClickListener(this); scroll = (ScrollView) findViewById(R.id.scroll); scroll.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_MOVE: if(scroll.getScrollY()<=0){ textMark.setText("您已经滑到最顶部了"); } if(scroll.getChildAt(0).getMeasuredHeight() <= scroll .getHeight() + scroll.getScrollY()){ textMark.setText("您已经滑到最底部了"); //textView.append(getResources().getString(R.string.content)); } break; } return false; } }); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { //scrollTo:以滚动视图起始位置开始计算的 //scrollBy:相对前一次的位置,去滚动对应的距离 case R.id.up: { scroll.scrollBy(0, -30); break; } case R.id.down: { scroll.scrollBy(0, 30); break; } } } }布局文件
<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" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/mark" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:hint="提示" android:textColor="#ff1100" android:textSize="25sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:orientation="horizontal" > <Button android:id="@+id/up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UP" /> <Button android:id="@+id/down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DOWN" /> </LinearLayout> </RelativeLayout> <ScrollView android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" /> </ScrollView> </LinearLayout>实现效果如下:
相关文章推荐
- StringTokenizer的介绍以及简单的使用demo
- Face++在线接口在Android下的使用,以及简单的Demo实现(判断两张脸是否为同一个人)
- Face++在线接口在Android下的使用,以及简单的Demo实现(判断两张脸是否为同一个人)
- javascript入门系列演示·函数的定义以及简单参数使用,调用函数
- lr_eval_string()函数以及LR中参数、变量的简单使用
- php-memcache 的安装以及简单使用
- 浅析Java执行外部命令的几个要点(1)——简单的使用范例以及在Cygwin上的注意点
- javascript入门系列演示·函数的定义以及简单参数使用,调用函数
- 【Android游戏开发二十一】Android os设备谎言分辨率的解决方案!以及简单阐述游戏引擎如何使用!
- NFS环境的架设,以及samba简单使用
- Android os设备谎言分辨率的解决方案!以及简单阐述游戏引擎如何使用!
- FileDialog简单使用demo
- 在Xcode以及Interface Builder下简单使用Cocoa (更新版)
- APIDemo学习笔记——Android上几种简单的Animation使用方法(二)
- APIDemo学习笔记——Android上几种简单的Animation使用方法(三)——3D Transition
- FileDialog简单使用demo
- javascript入门系列演示·函数的定义以及简单参数使用,调用函数
- javascript入门系列演示·函数的定义以及简单参数使用,调用函数 .txt
- 记录Windows Service简单DEMO以及配置..
- 简单的go以及begin和end使用