Android实战之Scrollbar Activity


Android中几个常用的Layout虽然有scrollbar属性,但是并不能实现当其中内容太多时自动在Activity上出现Scrollbar,后来发现对于这种情况其实是需要使用ScrollView来处理的,具体配置可以如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="wrap_content" >  
  6.         <ScrollView    
  7.             android:layout_width="fill_parent"    
  8.             android:layout_height="wrap_content" >  
  9.             <LinearLayout    
  10.                 android:id="@+id_scrollbar/layout1"  
  11.                 android:orientation="vertical"    
  12.                 android:layout_width="fill_parent"    
  13.                 android:layout_height="wrap_content" >  
  14.                 ...   
  15.             </LinearLayout>  
  16.     </ScrollView>  
  17. </LinearLayout>   

或者

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="wrap_content" >  
  5.     <LinearLayout    
  6.         android:orientation="vertical"    
  7.         android:layout_width="fill_parent"    
  8.         android:layout_height="wrap_content" >  
  9.         ...   
  10.     </LinearLayout>  
  11. </ScrollView>   

其中需要注意的是ScrollView内部只能有一个子元素,所以需要把所有的子元素放到一个LinearLayout内部。

相关内容