Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> android之ScrollView

android之ScrollView

編輯:Android開發實例

  當您TextView中的字數太多,不足以在屏幕上完整顯示時,如何使一個TextView用ScrollView滾動顯示呢?

  下面我舉一個簡單的例子:

  代碼:

 

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.IOException;  
  4. import org.apache.http.util.EncodingUtils;  
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.ScrollView;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12. public class pass extends Activity {  
  13.     /** Called when the activity is first created. */ 
  14.     final String TEXT_ENCODING = "UTF-8";  
  15.     ScrollView sv;  
  16.     TextView tv;  
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.passdialog);  
  21.         sv=(ScrollView)findViewById(R.id.sv);  
  22.         tv=(TextView)findViewById(R.id.txtView);  
  23.         tv.setText("mmmmmmmmmmmmmmmmmmmmm" +  
  24.           "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +  
  25.           "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll+  
  26.           "gggggggggggggggggggggggggggggggggggggggggggggggggg" +  
  27.           "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv," +  
  28.           "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" +  
  29.           "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");  
  30.     }  
  31. }  
  32.  
  33. passdialog.xml:  
  34.  
  35. <?xml version="1.0" encoding="utf-8"?>  
  36. <AbsoluteLayout  
  37.     android:id="@+id/widget38" 
  38.     android:layout_width="fill_parent" 
  39.     android:layout_height="fill_parent" 
  40.     xmlns:android="http://schemas.android.com/apk/res/android" 
  41. >  
  42.     <LinearLayout  
  43.         android:id="@+id/widget32" 
  44.         android:layout_width="wrap_content" 
  45.         android:layout_height="wrap_content" 
  46.         xmlns:android="http://schemas.android.com/apk/res/android" 
  47.         android:layout_x="0px" 
  48.         android:layout_y="0px" 
  49.     >  
  50.     </LinearLayout>  
  51.     <ScrollView  
  52.     android:id="@+id/sv" 
  53.     android:layout_width="wrap_content" 
  54.     android:layout_height="wrap_content" 
  55.     xmlns:android="http://schemas.android.com/apk/res/android" 
  56.     android:layout_x="0px" 
  57.     android:layout_y="20px" 
  58.     >  
  59.         <TextView  
  60.         android:id="@+id/txtView" 
  61.         android:layout_width="wrap_content" 
  62.         android:layout_height="wrap_content" 
  63.         android:textSize="22sp" 
  64.         android:gravity="center_vertical" 
  65.         >  
  66.         </TextView>  
  67.     </ScrollView>  
  68. </AbsoluteLayout>  

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved