当屏幕内容较多无法全部显示时,就需要用到滚动视图。
种类:
- HorizontalScrollView:水平滚动视图
- ScorllView:垂直滚动视图
标签中的Scrollbars可以这只滚动条的属性,能让它是否显示
方法getScaleY()可以获取滚动过的距离
scrollto 和 scrollby 方法的区别
//scrollto 以滚动视图起始位置开始计算的
//Scrollby 以前一个位置的开始计算,滚动对应位置
example:
------------------------------------------------------------------------------------------------------------
public class MainActivity extends Activity implements OnClickListener{
private TextView tv;
private ScrollView scroll;
private Button bt1;
private Button bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textview);
tv.setText(R.string.Text);
bt1 = (Button) findViewById(R.id.bt_up);
bt2 = (Button) findViewById(R.id.bt_down);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
scroll = (ScrollView) findViewById(R.id.scroll);
scroll.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
/*
*1. getScrollY -- 滚动条滚动的距离
* 2. getmeasureHeight-- 是整个内容的高度
* 3. getHeight--- 屏幕的高度
*/
case MotionEvent.ACTION_MOVE:{
if(scroll.getScaleY()<=0)
{
Log.i("main", "滑动到顶部");
}
if(scroll.getChildAt(0).getMeasuredHeight()<=scroll.getHeight()+scroll.getScaleY()){
Log.i("main","滑动到底部");
tv.append(getResources().getText(R.string.Text));
}
break;
}
default:
break;
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt_up:{
scroll.scrollBy(0,-60);
break;
}
case R.id.bt_down:{
scroll.scrollBy(0, 60);
break;
}
default:
break;
}
}
}