ScrollView在android中能夠實現滾動視圖,手機屏幕大小有限,當顯示的內容較多時,滾動視圖就派上用場了。java
該實例主要是經過佈局文件生成視圖,只是演示ScrollView的使用,沒有考慮總體美觀效果。整體來看,採用垂直佈局,在垂直佈局的第一行加入一個TextView組件;第二行是一個ScrollView,內部加入垂直佈局;第三行再加入一個TextView組件。android
在佈局文件中使用ScrollView,通常是在ScrollView節點中加入佈局方式,以下所示,ScrollView也能夠做爲根節點。app
<ScrollView ... >ide
<LinearLayout ...>
佈局
...
測試
</ LinearLayout>
code
</ ScrollView>xml
首先是佈局文件:utf-8
<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文件:string
<?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); } }
最後是測試結果: