zoukankan      html  css  js  c++  java
  • android scrollview 简单的使用

    以前写的Scrollview ,通常都是与Listview结合使用,不过因复杂可能新手不太懂,网上有许多文章,这里就不贴那个了DEMO了. 

    写了个简单的供大家参考:这样比较好理解(需要复杂的可以Q我,这里的博客不经常上的)

    出处:http://blog.csdn.net/djy1992/article/details/9223019


    首先是JAVA主代码:

     
    package com.dudu.djy;   
     
    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;   
     

              /***
        * scrollview
        * @author dujinyang
       *
       */
    public class ScrollViewTests extends Activity {   
       /** Called when the activity is first created. */  
       private LinearLayout mLayout;   
       private ScrollView scView;   
       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);    
           scView= (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 bt = (Button) this.getCurrentFocus();   
           int count = mLayout.getChildCount();   
           Button bm = (Button) mLayout.getChildAt(count-1);   
     
           if(keyCode==KeyEvent.KEYCODE_DPAD_UP && bt .getId()==R.id.Button){   
               bm.requestFocus();   
               return true;   
           }else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN && bt .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 tv= new TextView(ScrollViewTest.this);//定义一个TextView   
               tView.setText("TextView" + index);//设置TextView的文本信息   
               //设置线性布局的属性   
               LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(   
                       LinearLayout.LayoutParams.FILL_PARENT,   
                       LinearLayout.LayoutParams.WRAP_CONTENT);   
               mLayout.addView(tv, params);//添加一个TextView控件   
               Button button = new Button(ScrollViewTest.this);//定义一个Button   
               button.setText("Button" + index);//设置Button的文本信息   
               button.setId(index++);//id   
               mLayout.addView(button, params);//添加一个Button控件   
               mHandler.post(mScrollToButton);//传递一个消息进行滚动   
           }   
     
       };   


      //传递一个消息进行滚动 
       private Runnable mScrollToButton = new Runnable() {   
     
           @Override  
           public void run() {   
               int off = mLayout.getMeasuredHeight() - scView.getHeight();   
               if (off > 0) {   
                   scView.scrollTo(0, off);//改变滚动条的位置   
               }   
           }    
       };    
     
    }  


    然后是main.xml文件:


    <?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>  


    代码共享完成.

  • 相关阅读:
    12.4案例分析:NASAECS项目
    第12章 CBAM:构架设计决策制定的定量方法
    11.4 Nightingale系统:应用ATAM的案例分析
    第11章 ATAM:一种进行构架评估的综合方法
    第Ⅲ部分 分析构架
    第10章 软件构架重构
    9.5跨视图的文档
    第9章 构架编档
    基于Spring MVC的Web应用开发(三)
    Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3167713.html
Copyright © 2011-2022 走看看