Android学习笔记:ScrollView卷轴视图
生活随笔
收集整理的這篇文章主要介紹了
Android学习笔记:ScrollView卷轴视图
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
ScrollView卷軸視圖是指當擁有很多內(nèi)容,一屏顯示不完時,需要通過滾動跳來顯示的視圖.的使用:
Java代碼<?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>??
<?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>
Java代碼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); ?? ????????//?創(chuàng)建一個線性布局?? ????????mLayout?=?(LinearLayout)?this.findViewById(R.id.LinearLayout); ?? ????????//?創(chuàng)建一個ScrollView對象?? ????????sView?=?(ScrollView)?this.findViewById(R.id.ScrollView); ?? ????????Button?mBtn?=?(Button)?this.findViewById(R.id.Button); ?? ????????mBtn.setOnClickListener(mClickListener);//?添加點擊事件監(jiān)聽?? ????} ?? ?? ????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事件監(jiān)聽,當點擊第一個按鈕時增加一個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);//改變滾動條的位置?? ????????????} ?? ????????} ?? ?? ????}; ?? ?? ?? }??
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);// 創(chuàng)建一個線性布局mLayout = (LinearLayout) this.findViewById(R.id.LinearLayout);// 創(chuàng)建一個ScrollView對象sView = (ScrollView) this.findViewById(R.id.ScrollView);Button mBtn = (Button) this.findViewById(R.id.Button);mBtn.setOnClickListener(mClickListener);// 添加點擊事件監(jiān)聽}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事件監(jiān)聽,當點擊第一個按鈕時增加一個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來實現(xiàn)自動滾動,當我們點擊Button0時自動產(chǎn)生多個類似的項,如果一屏顯示不完,則通過ScrollView來顯示。
Java代碼
Java代碼
此示例中一個TextView和一個Button來實現(xiàn)自動滾動,當我們點擊Button0時自動產(chǎn)生多個類似的項,如果一屏顯示不完,則通過ScrollView來顯示。
總結(jié)
以上是生活随笔為你收集整理的Android学习笔记:ScrollView卷轴视图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android学习笔记:Activity
- 下一篇: Android属性之build.prop