失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Android学习笔记:ScrollView卷轴视图

Android学习笔记:ScrollView卷轴视图

时间:2024-06-12 22:55:50

相关推荐

Android学习笔记:ScrollView卷轴视图

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

Java代码 <?xmlversion="1.0"encoding="utf-8"?> <ScrollViewxmlns:android="/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>

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="/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>

Java代码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(); @OverridepublicvoidonCreate(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和一个textviewprivateButton.OnClickListenermClickListener=newButton.OnClickListener(){privateintindex=1; @OverridepublicvoidonClick(Viewv){ TextViewtView=newTextView(Test_ScrollView.this);//定义一个TextViewtView.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);//定义一个Buttonbutton.setText("Button"+index);//设置Button的文本信息button.setId(index++); mLayout.addView(button,params);//添加一个Button控件mHandler.post(mScrollToButton);//传递一个消息进行滚动} };privateRunnablemScrollToButton=newRunnable(){ @Overridepublicvoidrun(){intoff=mLayout.getMeasuredHeight()-sView.getHeight();if(off>0){ sView.scrollTo(0,off);//改变滚动条的位置} } }; }

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();@Overridepublic 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和一个textviewprivate Button.OnClickListener mClickListener = new Button.OnClickListener() {private int index = 1;@Overridepublic void onClick(View v) {TextView tView = new TextView(Test_ScrollView.this);//定义一个TextViewtView.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);//定义一个Buttonbutton.setText("Button" + index);//设置Button的文本信息button.setId(index++);mLayout.addView(button, params);//添加一个Button控件mHandler.post(mScrollToButton);//传递一个消息进行滚动}};private Runnable mScrollToButton = new Runnable() {@Overridepublic void run() {int off = mLayout.getMeasuredHeight() - sView.getHeight();if (off > 0) {sView.scrollTo(0, off);//改变滚动条的位置}}};}

此示例中一个TextView和一个Button来实现自动滚动,当我们点击Button0时自动产生多个类似的项,如果一屏显示不完,则通过ScrollView来显示。

如果觉得《Android学习笔记:ScrollView卷轴视图》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。