用户界面View之ScrollView

本文详细介绍了ScrollView和HorizontalScrollView的滚动原理及如何通过XML属性和Java代码实现滚动条的隐藏,同时提供了实例代码演示如何通过触摸事件监听器控制滚动视图的滚动行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



心念一转,万念皆转;心路一通,万路皆通。


本讲内容: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 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值