Android(安卓)ScrollView的具体使用
16lz
2021-01-24
ScrollView卷轴视图是指当拥有很多内容,一屏显示不完时,需要通过滚动跳来显示的视图.的使用:
- <?xmlversion="1.0"encoding="utf-8"?>
- <ScrollViewxmlns: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">
- <LinearLayoutandroid:id="@+id/LinearLayout"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextViewandroid:id="@+id/TestView"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="TestView0"/>
- <Buttonandroid:id="@+id/Button"android:text="Button0"android:layout_width="fill_parent"
- android:layout_height="wrap_content"></Button>
- </LinearLayout>
- </ScrollView>
- packagecom.Aina.Android;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.view.KeyEvent;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.LinearLayout;
- importandroid.widget.ScrollView;
- importandroid.widget.TextView;
- publicclassTest_ScrollViewextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- privateLinearLayoutmLayout;
- privateScrollViewsView;
- privatefinalHandlermHandler=newHandler();
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //创建一个线性布局
- mLayout=(LinearLayout)this.findViewById(R.id.LinearLayout);
- //创建一个ScrollView对象
- sView=(ScrollView)this.findViewById(R.id.ScrollView);
- ButtonmBtn=(Button)this.findViewById(R.id.Button);
- mBtn.setOnClickListener(mClickListener);//添加点击事件监听
- }
- publicbooleanonKeyDown(intkeyCode,KeyEventevent){
- Buttonb=(Button)this.getCurrentFocus();
- intcount=mLayout.getChildCount();
- Buttonbm=(Button)mLayout.getChildAt(count-1);
- if(keyCode==KeyEvent.KEYCODE_DPAD_UP&&b.getId()==R.id.Button){
- bm.requestFocus();
- returntrue;
- }elseif(keyCode==KeyEvent.KEYCODE_DPAD_DOWN&&b.getId()==bm.getId()){
- this.findViewById(R.id.Button).requestFocus();
- returntrue;
- }
- returnfalse;
- }
- //Button事件监听,当点击第一个按钮时增加一个button和一个textview
- privateButton.OnClickListenermClickListener=newButton.OnClickListener(){
- privateintindex=1;
- @Override
- publicvoidonClick(Viewv){
- TextViewtView=newTextView(Test_ScrollView.this);//定义一个TextView
- tView.setText("TextView"+index);//设置TextView的文本信息
- //设置线性布局的属性
- LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- mLayout.addView(tView,params);//添加一个TextView控件
- Buttonbutton=newButton(Test_ScrollView.this);//定义一个Button
- button.setText("Button"+index);//设置Button的文本信息
- button.setId(index++);
- mLayout.addView(button,params);//添加一个Button控件
- mHandler.post(mScrollToButton);//传递一个消息进行滚动
- }
- };
- privateRunnablemScrollToButton=newRunnable(){
- @Override
- publicvoidrun(){
- intoff=mLayout.getMeasuredHeight()-sView.getHeight();
- if(off>0){
- sView.scrollTo(0,off);//改变滚动条的位置
- }
- }
- };
- }
此示例中一个TextView和一个Button来实现自动滚动,当我们点击Button0时自动产生多个类似的项,如果一屏显示不完,则通过ScrollView来显示。 在实际使用中,只要<ScrollView></ScrollView>包住LinearLayout即可
更多相关文章
- Android(安卓)7.1.1 去除Launcher3的抽屉(2018/12/5)
- 更新进度条
- Google Map V2在Android中的应用
- Android点击button触发Toast事件,弹出一个小小的消息框,几秒钟之
- LinearLayout和RelativeLayout的属性差异
- Android(安卓)EditText 密码隐藏或可见
- Android单元测试框架-Espresso
- ScrollView can host only one direct child
- Android之WebView控件简单使用总结