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

Android ScrollView的使用

jsc9年前 (2016-04-06)Android3696

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的使用” 的相关文章

创建Popwindow弹出菜单的两种方式

创建Popwindow弹出菜单的两种方式

方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.v...

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

注:(图中每一个条目和图标都是由代码动态生成) 代码动态布局,并需要为每一个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon" ...

Android ImageView的scaleType属性与adjustViewBounds属性

Android ImageView的scaleType属性与adjustViewBounds属性

ImageView的scaleType的属性有好几种,分别是matrix(默认)、center、centerCrop、centerInside、fitCenter、fitEnd、fitStart、fitXY android:sca...

Android中从SD卡中/拍照选择图片并进行剪裁的方法

Android中从SD卡中/拍照选择图片并进行剪裁的方法

效果图: 下面是代码的部分,部分是从网路上摘录的,自己整理后当做工具类使用   配置文件:布局很简单,一个ImageButton和一个Button,点击都可以实现图像选择的功能,具体的实现根据大家在实际中...

Android 更换皮肤思路及解决方案

Android 更换皮肤思路及解决方案

本篇博客要给大家分享的一个关于Android应用换肤的Demo,大家可以到我的github去下载demo,以后博文涉及到的代码均会上传到github中统一管理。 github地址:https://github.com/devilWwj/Android-skin-upda...

Android中的消息通知(NotificationManager和Notification)

下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提 示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个快讯。已添加的Notification.Builder,使其更容易构建通知。 notifica...