android常用組件之ScrollView

來源:互聯網
上載者:User

標籤:

ScrollView在android中可以實現滾動視圖,手機螢幕大小有限,當顯示的內容較多時,滾動視圖就派上用場了。

該執行個體主要是通過布局檔案產生視圖,只是示範ScrollView的使用,沒有考慮整體美觀效果。總體來看,採用垂直布局,在垂直布局的第一行加入一個TextView組件;第二行是一個ScrollView,內部加入垂直布局;第三行再加入一個TextView組件。

在布局檔案中使用ScrollView,一般是在ScrollView節點中加入布局方式,如下所示,ScrollView也可以作為根節點。

<ScrollView ... >

   <LinearLayout ...>

       ...

  </ LinearLayout>

</ ScrollView>

首先是布局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="main.test_scrollview.MainActivity" >    <TextView        android:id="@+id/textView1"        android:layout_width="fill_parent"        android:layout_height="100sp"        android:background="#ff0000ff"        android:textColor="#ffffffff"        android:text="@string/tv1"        android:gravity="center"/>    <ScrollView         android:id="@+id/scrollView"        android:layout_width="wrap_content"        android:layout_height="200dp">       <LinearLayout            android:id="@+id/linearLayout2"           android:layout_width="fill_parent"           android:layout_height="wrap_content"           android:orientation="vertical">           <ProgressBar               android:id="@+id/progressBar1"               style="?android:attr/progressBarStyleLarge"               android:layout_width="wrap_content"               android:layout_height="wrap_content" />           <ProgressBar               android:id="@+id/progressBar2"               style="?android:attr/progressBarStyleLarge"               android:layout_width="wrap_content"               android:layout_height="wrap_content" />           <View                android:id="@+id/view1"               android:layout_width="wrap_content"               android:layout_height="100sp"               android:background="#ff0" />           <ProgressBar               android:id="@+id/progressBar3"               style="?android:attr/progressBarStyleLarge"               android:layout_width="wrap_content"               android:layout_height="wrap_content" />           <ProgressBar               android:id="@+id/progressBar4"               style="?android:attr/progressBarStyleLarge"               android:layout_width="wrap_content"               android:layout_height="wrap_content" />           <View                android:id="@+id/view2"               android:layout_width="wrap_content"               android:layout_height="100sp"               android:background="#f00" />           <!-- 自訂了一個View組件顯示 -->           <ProgressBar               android:id="@+id/progressBar5"               style="?android:attr/progressBarStyleLarge"               android:layout_width="wrap_content"               android:layout_height="wrap_content" />           <ProgressBar               android:id="@+id/progressBar6"               style="?android:attr/progressBarStyleLarge"               android:layout_width="wrap_content"               android:layout_height="wrap_content" />       </LinearLayout>     </ScrollView>    <TextView        android:id="@+id/textView2"        android:layout_width="fill_parent"        android:layout_height="100sp"        android:background="#ff0000ff"        android:gravity="center"        android:textColor="#ffffffff"        android:text="@string/tv2" />       </LinearLayout>

其次是strings.xml檔案:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Test_ScrollView</string>    <string name="tv1">上邊界</string>    <string name="action_settings">Settings</string>    <string name="tv2">下邊界</string></resources>

再次是android原始碼檔案(只是引用了布局檔案,其他沒有任何改動):

package main.test_scrollview;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;public class MainActivity extends ActionBarActivity {        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          }}

最後是測試結果:

android常用組件之ScrollView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.