Android学习笔记:ScrollView卷轴视图

ScrollView卷轴视图是指当拥有很多内容,一屏显示不完时,需要通过滚动跳来显示的视图.的使用:

Java代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/ScrollView" android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" android:scrollbars="vertical">   
  5.     <LinearLayout android:id="@+id/LinearLayout"  
  6.         android:orientation="vertical" android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content">   
  8.         <TextView android:id="@+id/TestView" android:layout_width="fill_parent"  
  9.             android:layout_height="wrap_content" android:text="TestView0" />   
  10.         <Button android:id="@+id/Button" android:text="Button0" android:layout_width="fill_parent"  
  11.             android:layout_height="wrap_content"></Button>   
  12.     </LinearLayout>   
  13. </ScrollView>  
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/ScrollView" android:layout_width="fill_parent"
	android:layout_height="wrap_content" android:scrollbars="vertical">
	<LinearLayout android:id="@+id/LinearLayout"
		android:orientation="vertical" android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView android:id="@+id/TestView" android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:text="TestView0" />
		<Button android:id="@+id/Button" android:text="Button0" android:layout_width="fill_parent"
			android:layout_height="wrap_content"></Button>
	</LinearLayout>
</ScrollView>


Java代码
  1. package com.Aina.Android;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.os.Handler;   
  6. import android.view.KeyEvent;   
  7. import android.view.View;   
  8. import android.widget.Button;   
  9. import android.widget.LinearLayout;   
  10. import android.widget.ScrollView;   
  11. import android.widget.TextView;   
  12.   
  13. public class Test_ScrollView extends Activity {   
  14.     /** Called when the activity is first created. */  
  15.     private LinearLayout mLayout;   
  16.     private ScrollView sView;   
  17.     private final Handler mHandler = new Handler();   
  18.   
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {   
  21.         super.onCreate(savedInstanceState);   
  22.         setContentView(R.layout.main);   
  23.         // 创建一个线性布局  
  24.         mLayout = (LinearLayout) this.findViewById(R.id.LinearLayout);   
  25.         // 创建一个ScrollView对象  
  26.         sView = (ScrollView) this.findViewById(R.id.ScrollView);   
  27.         Button mBtn = (Button) this.findViewById(R.id.Button);   
  28.         mBtn.setOnClickListener(mClickListener);// 添加点击事件监听  
  29.     }   
  30.   
  31.     public boolean onKeyDown(int keyCode, KeyEvent event){   
  32.         Button b = (Button) this.getCurrentFocus();   
  33.         int count = mLayout.getChildCount();   
  34.         Button bm = (Button) mLayout.getChildAt(count-1);   
  35.   
  36.         if(keyCode==KeyEvent.KEYCODE_DPAD_UP && b.getId()==R.id.Button){   
  37.             bm.requestFocus();   
  38.             return true;   
  39.         }else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN && b.getId()==bm.getId()){   
  40.             this.findViewById(R.id.Button).requestFocus();   
  41.             return true;   
  42.         }   
  43.         return false;   
  44.     }   
  45.     // Button事件监听,当点击第一个按钮时增加一个button和一个textview  
  46.     private Button.OnClickListener mClickListener = new Button.OnClickListener() {   
  47.   
  48.         private int index = 1;   
  49.   
  50.         @Override  
  51.         public void onClick(View v) {   
  52.             TextView tView = new TextView(Test_ScrollView.this);//定义一个TextView  
  53.             tView.setText("TextView" + index);//设置TextView的文本信息  
  54.             //设置线性布局的属性  
  55.             LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(   
  56.                     LinearLayout.LayoutParams.FILL_PARENT,   
  57.                     LinearLayout.LayoutParams.WRAP_CONTENT);   
  58.             mLayout.addView(tView, params);//添加一个TextView控件  
  59.             Button button = new Button(Test_ScrollView.this);//定义一个Button  
  60.             button.setText("Button" + index);//设置Button的文本信息  
  61.             button.setId(index++);   
  62.             mLayout.addView(button, params);//添加一个Button控件  
  63.             mHandler.post(mScrollToButton);//传递一个消息进行滚动  
  64.         }   
  65.   
  66.     };   
  67.     private Runnable mScrollToButton = new Runnable() {   
  68.   
  69.         @Override  
  70.         public void run() {   
  71.             int off = mLayout.getMeasuredHeight() - sView.getHeight();   
  72.             if (off > 0) {   
  73.                 sView.scrollTo(0, off);//改变滚动条的位置  
  74.             }   
  75.         }   
  76.   
  77.     };   
  78.   
  79.   
  80. }  
package com.Aina.Android;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class Test_ScrollView extends Activity {
	/** Called when the activity is first created. */
	private LinearLayout mLayout;
	private ScrollView sView;
	private final Handler mHandler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 创建一个线性布局
		mLayout = (LinearLayout) this.findViewById(R.id.LinearLayout);
		// 创建一个ScrollView对象
		sView = (ScrollView) this.findViewById(R.id.ScrollView);
		Button mBtn = (Button) this.findViewById(R.id.Button);
		mBtn.setOnClickListener(mClickListener);// 添加点击事件监听
	}

	public boolean onKeyDown(int keyCode, KeyEvent event){
		Button b = (Button) this.getCurrentFocus();
		int count = mLayout.getChildCount();
		Button bm = (Button) mLayout.getChildAt(count-1);

		if(keyCode==KeyEvent.KEYCODE_DPAD_UP && b.getId()==R.id.Button){
			bm.requestFocus();
			return true;
		}else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN && b.getId()==bm.getId()){
			this.findViewById(R.id.Button).requestFocus();
			return true;
		}
		return false;
	}
	// Button事件监听,当点击第一个按钮时增加一个button和一个textview
	private Button.OnClickListener mClickListener = new Button.OnClickListener() {

		private int index = 1;

		@Override
		public void onClick(View v) {
			TextView tView = new TextView(Test_ScrollView.this);//定义一个TextView
			tView.setText("TextView" + index);//设置TextView的文本信息
			//设置线性布局的属性
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			mLayout.addView(tView, params);//添加一个TextView控件
			Button button = new Button(Test_ScrollView.this);//定义一个Button
			button.setText("Button" + index);//设置Button的文本信息
			button.setId(index++);
			mLayout.addView(button, params);//添加一个Button控件
			mHandler.post(mScrollToButton);//传递一个消息进行滚动
		}

	};
	private Runnable mScrollToButton = new Runnable() {

		@Override
		public void run() {
			int off = mLayout.getMeasuredHeight() - sView.getHeight();
			if (off > 0) {
				sView.scrollTo(0, off);//改变滚动条的位置
			}
		}

	};


}


此示例中一个TextView和一个Button来实现自动滚动,当我们点击Button0时自动产生多个类似的项,如果一屏显示不完,则通过ScrollView来显示。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值