【Android 开发教程】ScrollView滚动视图

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


ScrollView是一种特殊的FrameLayout,使用ScrollView可以使用户能够滚动一个包含views的列表,这样做的话,就可以利用比物理显示区域更大的空间。有一点需要注意一下,那就是ScrollView只能包含一个子视图view或ViewGroup(这个ViewGroup通常是LinearLayout)。

不要混合使用ListView和ScrollView。ListView被设计用来显示一些相关的信息,同时,ListView也已经被优化了去显示大量的列表lists。

下面的main.xml显示了一个包含LinearLayout的ScrollView,在LinearLayuout中又包含了一些Button和EditText视图:

[html] view plain copy
  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="fill_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:orientation="vertical" >  
  10.   
  11.         <Button  
  12.             android:id="@+id/button1"  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="Button1" />  
  16.   
  17.         <Button  
  18.             android:id="@+id/button2"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:text="Button2" />  
  22.   
  23.         <Button  
  24.             android:id="@+id/button3"  
  25.             android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="Button3" />  
  28.   
  29.         <EditText  
  30.             android:id="@+id/txt"  
  31.             android:layout_width="fill_parent"  
  32.             android:layout_height="600dp" />  
  33.   
  34.         <Button  
  35.             android:id="@+id/button4"  
  36.             android:layout_width="fill_parent"  
  37.             android:layout_height="wrap_content"  
  38.             android:text="Button4" />  
  39.   
  40.         <Button  
  41.             android:id="@+id/button5"  
  42.             android:layout_width="fill_parent"  
  43.             android:layout_height="wrap_content"  
  44.             android:text="Button5" />  
  45.     </LinearLayout>  
  46.   
  47. </ScrollView>  
以上代码在模拟器上的效果图:

因为EditText自动获得了焦点,它充满了整个activity(因为它的高度被设置成了600dp)。如果想阻止这个EditText获得焦点,那么只需在<LinearLayout>元素中添加以下两个属性:

[html] view plain copy
  1. <LinearLayout  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:orientation="vertical"  
  5.       
  6.     android:focusable="true"  
  7.     android:focusableInTouchMode="true"  
  8.      >  
现在,就可以看到那些button按钮视图了,同时也可以滚动这些视图列表。就像下面展示的那样:

但是有的时候可能想要这个EditText自动获取焦点,但是又不想软键盘自动地显示出来。想要阻止软键盘的出现,可以在AndroidManifext.xml中的<activity>节点中,添加如下的属性:

[html] view plain copy
  1. <activity  
  2.             android:name=".LayoutActivity"  
  3.             android:label="@string/app_name"   
  4.             <!-- 注意这行代码 -->  
  5.             android:windowSoftInputMode="stateHidden"  
  6.             >  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.MAIN" />  
  9.   
  10.                 <category android:name="android.intent.category.LAUNCHER" />  
  11.             </intent-filter>  
  12.         </activity>  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值