您的位置:首页 > 其它

ScrollView的使用以及简单Demo

2015-01-31 17:24 218 查看
ScrollView 就是我们常见的垂直滚动条

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>
实现效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: