【Android 开发教程】ScrollView滚动视图
2012-05-18 10:14
344 查看
本章节翻译自《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 plaincopy<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button3" />
<EditText
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="600dp" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</ScrollView>
以上代码在模拟器上的效果图:
因为EditText自动获得了焦点,它充满了整个activity(因为它的高度被设置成了600dp)。如果想阻止这个EditText获得焦点,那么只需在<LinearLayout>元素中添加以下两个属性:
[html] view plaincopy<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
>
现在,就可以看到那些button按钮视图了,同时也可以滚动这些视图列表。就像下面展示的那样:
但是有的时候可能想要这个EditText自动获取焦点,但是又不想软键盘自动地显示出来。想要阻止软键盘的出现,可以在AndroidManifext.xml中的<activity>节点中,添加如下的属性:
[html] view plaincopy<activity
android:name=".LayoutActivity"
android:label="@string/app_name"
<!-- 注意这行代码 -->
android:windowSoftInputMode="stateHidden"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
原书购买地址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 plaincopy<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button3" />
<EditText
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="600dp" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</ScrollView>
以上代码在模拟器上的效果图:
因为EditText自动获得了焦点,它充满了整个activity(因为它的高度被设置成了600dp)。如果想阻止这个EditText获得焦点,那么只需在<LinearLayout>元素中添加以下两个属性:
[html] view plaincopy<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
>
现在,就可以看到那些button按钮视图了,同时也可以滚动这些视图列表。就像下面展示的那样:
但是有的时候可能想要这个EditText自动获取焦点,但是又不想软键盘自动地显示出来。想要阻止软键盘的出现,可以在AndroidManifext.xml中的<activity>节点中,添加如下的属性:
[html] view plaincopy<activity
android:name=".LayoutActivity"
android:label="@string/app_name"
<!-- 注意这行代码 -->
android:windowSoftInputMode="stateHidden"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
相关文章推荐
- android开发教程之使用线程实现视图平滑滚动示例 改
- Android开发总结笔记 ScrollView和HorizontalScrollView (滚动视图) 1-3-5
- 步步为营_Android开发课[25]_用户界面之ScrollView(滚动视图)
- Android 程式开发:(五)屏幕组件 —— 5.7 ScrollView滚动视图
- android开发教程之使用线程实现视图平滑滚动示例
- Android开发高级组件--ScrollView(滚动视图组件)
- android开发教程之使用线程实现视图平滑滚动示例
- scrollview中android:fillviewpoint="true"内容不足以滚动视图时,使其内容撑满屏幕
- android-UI组件实例大全(十二)------ScrollView滚动视图
- Android开发实现带有反弹效果仿IOS反弹scrollview教程详解
- 从零开始学android<ScrollView滚动视图.十八.>
- 【Android 开发教程】锚定视图
- Android——滚动视图(ScrollView)图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟
- 从零开始学android:Android基本控件(下)—— 滚动视图:ScrollView
- android 滚动视图(ScrollView)
- android 滚动视图(ScrollView)学习与应用
- Android零基础入门第61节:滚动视图ScrollView
- 从零開始学android<ScrollView滚动视图.十八.>
- iOS 9应用开发教程之使用开关滑块控件以及滚动部署视图
- Unity3d UGUI 滚动视图ScrollView的使用教程之简易双肩包