心念一转,万念皆转;心路一通,万路皆通。
本讲内容:ScrollView 垂直滚动视图与HorizontalScrollView 水平滚动视图
一、隐藏滚动条
1、xml属性 : android:scrollbars="none";
2、java代码:setHorizontalScrollBarEnabled(false); setVerticalScrollBarEnabled(false);
示例一:
下面是res/layout/activity_main.xml 布局文件:
<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" >
<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" />
<ScrollView
android:id="@+id/id_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<TextView
android:id="@+id/id_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</ScrollView>
</LinearLayout>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity implements OnClickListener{
private TextView tv;
private ScrollView scroll;
private Button up;
private Button down;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
scroll.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
//手指落下
case MotionEvent.ACTION_DOWN:
break;
//手指滑动
case MotionEvent.ACTION_MOVE:
break;
//手指离开
case MotionEvent.ACTION_UP:
//顶部状态
if(scroll.getScrollY()==0){
Log.i("MainActivity", "滑动到顶部");
}
// 底部状态 TextView的总高度<=屏幕的高度+滚动条的滚动距离
if(scroll.getChildAt(0).getMeasuredHeight()==scroll.getHeight()+scroll.getScrollY()){
Log.i("MainActivity", "滑动到底部");
}
break;
}
return false;
}
});
}
/**
* 初始化控件
*/
private void initViews() {
tv=(TextView) findViewById(R.id.id_tv);
tv.setText(getResources().getString(R.string.content));
scroll=(ScrollView) findViewById(R.id.id_scroll);
up=(Button) findViewById(R.id.up);
down=(Button) findViewById(R.id.down);
up.setOnClickListener(this);
down.setOnClickListener(this);
}
/**
*按钮点击事件
*/
public void onClick(View v) {
switch (v.getId()) {
//scrollTo:到达
//scrollBy:滚动
case R.id.up:
scroll.scrollBy(0, -20);
break;
case R.id.down:
scroll.scrollBy(0, 20);
break;
}
}
}
Take your time and enjoy it