Android視圖之滾動視圖

來源:互聯網
上載者:User

標籤:android   scrollview   

滾動視圖(ScrollView)是當需要顯示的資訊一個螢幕顯示不下時使用的控制項。

1.ScrollView概述

ScrollView由FrameLayout派生,同樣位於android.widget包下。ScrollView類實際上是一個幀布局,一般情況下,其中的控制項是按照線性進行布局的,使用者可以對其進行滾動,以達到在螢幕中顯示更多資訊的目的。

預設情況下,ScrollView只是為其他組件添加垂直捲軸,如果應用需要添加水平捲軸,,則可藉助於另一個捲軸視圖來實現。ScrollView與HorizontalScrollView的準系統本相似,只是前者添加垂直捲軸,後者添加水平捲軸。

ScrollView的使用與普通布局的使用沒有太大區別,可以在XML檔案中進行配置,也可以通過java代碼進行設定。在ScrollView中可以添加任意滿足條件的控制項,當一個螢幕顯示不下其中所包含的所有控制項或資訊時,便會自動添加滾動功能。

注意:ScrollView中同一時刻只能包含一個View。

2.在XML中配置Scrollview控制項

下面給大家舉個例子來示範在XML檔案中配置Scrollview控制項。

利用Scrollview類完成圖片的垂直滾動。其實現的步驟為:

(1)在Eclipse中建立一個Android應用項目,命名為li_ScrollViewXML.

(2)開啟res/layout目錄下的main.xml檔案,在檔案中布局映像的垂直滾動,其代碼為:


<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android = "http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:scrollbars="vertical"    android:background="#000000">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/study"            android:layout_gravity="center_horizontal"/>    </LinearLayout></ScrollView>

3.在java檔案中配置Scrollview控制項


Scrollview捲軸視圖是指當擁有很多內容,一屏顯示不完時,需要通過捲軸來顯示視圖的使用。下面通過一個案例來示範在java檔案中配置Scrollview控制項。

【例】實現在java代碼中配置Scrollview控制項。其具體步驟為:

(1)在Eclipse中建立一個Android項目,命名為li_ScrollViewJava.

(2)開啟res/layout目錄下的main.xml布局檔案,在檔案中生命一個Scrollview控制項及一個Textview控制項,其代碼為:


<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android = "http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#000000">    <!--ScrollView為捲軸控制項;scrollbarStyle為捲軸的樣式-->    <ScrollView        android:layout_width="match_parent"        android:layout_height="200px"        android:scrollbarStyle="outsideOverlay"        android:background="#aabbcc">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:id="@+id/textview"/>    </ScrollView></LinearLayout>


(3)開啟src\fs.li_scrollviewjava包下的MainActivity.java檔案,在檔案中實現Scrollview的設定,其代碼為:


package com.duck.shiretan.scrollapplication;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        this.setContentView(R.layout.activity_main);        setTitle("ScrollView");        TextView textView = (TextView) findViewById(R.id.textview);        textView.setText("我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n 我是Android\n");    }}

運行上面的代碼得到的就是我們想要實現的Scrollview控制項的配置。


歡迎大家加入技術交流群:364595326


Android視圖之滾動視圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.