標籤:
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