当前位置:首页 > Android > 正文内容

Android ScrollView的使用

jsc9年前 (2016-04-06)Android3570

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"/>   
    </LinearLayout>   
</ScrollView>
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);//改变滚动条的位置   
            }   
        }   
  
    };   
  
  
}


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/82.html

标签: ScrollView
分享给朋友:

“Android ScrollView的使用” 的相关文章

Android中使用PULL方式解析XML文件

Android中使用PULL方式解析XML文件

 Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素 并触发相应事件。跟SAX不同的是, Pull解析器产生的事件是一个数字,而非方法,因此可以使用一个s...

TypedArray和obtainStyledAttributes使用

TypedArray和obtainStyledAttributes使用

在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用TextView的配置属性: <com.easymorse.textbutton.TextButto...

google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

    了解二维码这个东西还是从微信中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,不可思议啊,那时候还不了解二维码,呵呵,然后做项目的时候,老板说要加上二维码扫描功能,然后自己的屁颠屁颠的去百度,google...

实现应用程序只有在第一次启动时显示引导界面

第一次安装启动:启动页--->导航页-->主页面之后启动:启动页-->主页面实现的原理就是:在启动页面用做一个文件保存的状态,保存程序是不是第一次启动的状态。因为只是要保存一个状态,我们将这个程序是第一次打开就将他设为true,当他进入 主页面之后将他的状态未为false,因为都...

ViewPager + HorizontalScrollView 实现可滚动的标签栏

ViewPager + HorizontalScrollView 实现可滚动的标签栏

这是一个可滑动的标签栏的自定义控件,参考此文章http://blog.csdn.net/fx_sky/article/details/8990573,我将主要的功能整合成一个类,配上2个特定的布局即可使用。 效果图:    主要布局文件:<?xml&nb...

下拉刷新及滚动到底部加载更多的Listview使用

下拉刷新及滚动到底部加载更多的Listview使用

本文主要介绍可同时实现下拉刷新及滑动到底部加载更多的ListView的使用。 该ListView优点包括:a. 可自定义下拉响应事件(如下拉刷新)  b.可自定义滚动到底部响应的事件(如滑动到底部加载更多)  c.可自定义丰富的样式  d....