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

xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <ScrollViewxmlns: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. <LinearLayoutandroid:id="@+id/LinearLayout"
  6. android:orientation="vertical"android:layout_width="fill_parent"
  7. android:layout_height="wrap_content">
  8. <TextViewandroid:id="@+id/TestView"android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"android:text="TestView0"/>
  10. <Buttonandroid:id="@+id/Button"android:text="Button0"android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"></Button>
  12. </LinearLayout>
  13. </ScrollView>


Java代码
  1. packagecom.Aina.Android;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.os.Handler;
  5. importandroid.view.KeyEvent;
  6. importandroid.view.View;
  7. importandroid.widget.Button;
  8. importandroid.widget.LinearLayout;
  9. importandroid.widget.ScrollView;
  10. importandroid.widget.TextView;
  11. publicclassTest_ScrollViewextendsActivity{
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. privateLinearLayoutmLayout;
  14. privateScrollViewsView;
  15. privatefinalHandlermHandler=newHandler();
  16. @Override
  17. publicvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. //创建一个线性布局
  21. mLayout=(LinearLayout)this.findViewById(R.id.LinearLayout);
  22. //创建一个ScrollView对象
  23. sView=(ScrollView)this.findViewById(R.id.ScrollView);
  24. ButtonmBtn=(Button)this.findViewById(R.id.Button);
  25. mBtn.setOnClickListener(mClickListener);//添加点击事件监听
  26. }
  27. publicbooleanonKeyDown(intkeyCode,KeyEventevent){
  28. Buttonb=(Button)this.getCurrentFocus();
  29. intcount=mLayout.getChildCount();
  30. Buttonbm=(Button)mLayout.getChildAt(count-1);
  31. if(keyCode==KeyEvent.KEYCODE_DPAD_UP&&b.getId()==R.id.Button){
  32. bm.requestFocus();
  33. returntrue;
  34. }elseif(keyCode==KeyEvent.KEYCODE_DPAD_DOWN&&b.getId()==bm.getId()){
  35. this.findViewById(R.id.Button).requestFocus();
  36. returntrue;
  37. }
  38. returnfalse;
  39. }
  40. //Button事件监听,当点击第一个按钮时增加一个button和一个textview
  41. privateButton.OnClickListenermClickListener=newButton.OnClickListener(){
  42. privateintindex=1;
  43. @Override
  44. publicvoidonClick(Viewv){
  45. TextViewtView=newTextView(Test_ScrollView.this);//定义一个TextView
  46. tView.setText("TextView"+index);//设置TextView的文本信息
  47. //设置线性布局的属性
  48. LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(
  49. LinearLayout.LayoutParams.FILL_PARENT,
  50. LinearLayout.LayoutParams.WRAP_CONTENT);
  51. mLayout.addView(tView,params);//添加一个TextView控件
  52. Buttonbutton=newButton(Test_ScrollView.this);//定义一个Button
  53. button.setText("Button"+index);//设置Button的文本信息
  54. button.setId(index++);
  55. mLayout.addView(button,params);//添加一个Button控件
  56. mHandler.post(mScrollToButton);//传递一个消息进行滚动
  57. }
  58. };
  59. privateRunnablemScrollToButton=newRunnable(){
  60. @Override
  61. publicvoidrun(){
  62. intoff=mLayout.getMeasuredHeight()-sView.getHeight();
  63. if(off>0){
  64. sView.scrollTo(0,off);//改变滚动条的位置
  65. }
  66. }
  67. };
  68. }



此示例中一个TextView和一个Button来实现自动滚动,当我们点击Button0时自动产生多个类似的项,如果一屏显示不完,则通过ScrollView来显示。 在实际使用中,只要<ScrollView></ScrollView>包住LinearLayout即可

更多相关文章

  1. Android(安卓)7.1.1 去除Launcher3的抽屉(2018/12/5)
  2. 更新进度条
  3. Google Map V2在Android中的应用
  4. Android点击button触发Toast事件,弹出一个小小的消息框,几秒钟之
  5. LinearLayout和RelativeLayout的属性差异
  6. Android(安卓)EditText 密码隐藏或可见
  7. Android单元测试框架-Espresso
  8. ScrollView can host only one direct child
  9. Android之WebView控件简单使用总结

随机推荐

  1. 调用Android系统设置项
  2. android隐藏显示软键盘
  3. android:xml解析
  4. [转]ANDROID NOTIFICATIONS USING CORDOV
  5. android开关按钮,Switch,ToggleButton,Radio
  6. Android(安卓)判断是都联网状态
  7. Android之获取当前Activity名称
  8. android 从系统相册获取一张图片
  9. Android(安卓)控制软键盘的显示与隐藏
  10. 用什么Linux命令可以测试云服务器的网速/