Android学习之ScrollView使用文档翻译

来源:互联网 发布:环球慧思海关数据登陆 编辑:程序博客网 时间:2024/05/16 16:17

ScrollView是工作中常用的一个控件,相信大家都会使用,这是控件篇的最后一篇,所以也顺便翻译复习一下

文档翻译来源:

http://guides.codepath.com/android/Working-with-the-ScrollView

ScrollView的使用

当一个app的布局内容很长,比设备高度还长,这样内容应该会垂直滑动,这时我们就会用到ScrollView。

这里写图片描述

垂直滑动

要让内容垂直滑动,简单在ScrollView如下设置

To make any content vertically scrollable, simply wrap that content in a ScrollView :

<ScrollView  android:layout_width="match_parent"  android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >             <TextView              android:id="@+id/tv_long"              android:layout_width="wrap_content"              android:layout_height="match_parent"              android:text="@string/really_long_string" >          </TextView>          <Button              android:id="@+id/btn_act"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="View" >          </Button>    </LinearLayout></ScrollView>

ScrollView只能包含一个子元素如果你需要滑动很多控件的话。你需要把内容包装成一个布局如上所示。

在某些情况下,你期望的内容位置在滑动内容区域下方之后结束,例如一个服务条款,你只能接受,当你浏览完所有的内容,这样你需要设置android:fillViewport属性为true,

Note that a ScrollView can only contain a single child element so if you need multiple things to be scrollable, you need to wrap that content into a layout as shown above.

In certain situations, you want to position content beneath the end of the scrollable content area. For example for a “terms of service” where you can only accept once you’ve scrolled through all the content. In this case, you might need to apply the android:fillViewport property to “true”. Read this post by Romain Guy for a detailed look at this use case.

(ScrollView下面的组件如果有android:layout_height=”fill_parent”,那么必须将ScrollView的android:fillViewport属性设为true,不然高度撑不满整个ScrollView。)

滑动TextView

注意,一个TextView不需要ScrollView,如果你需要滑动一个TextView,设置scrollbars 属性和使用MovementMethod 方法。

Note that a TextView doesn’t require a ScrollView and if you just need a scrolling TextView simply set the scrollbars property and apply the correct MovementMethod :

<TextView    android:id="@+id/tv_long"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:scrollbars = "vertical"  android:text="@string/really_long_string" ></TextView>

在活动中如下设置

TextView tv = (TextView)findViewById(R.id.tv_long);tv.setMovementMethod(new ScrollingMovementMethod

这样TextView就可以自动垂直滑动了。

横向滑动

在其他情况下,我们希望内容横向滑动,我们可以这么使用HorizontalScrollView

In other cases, we want content to horizontally scroll in which case we need to use the HorizontalScrollView instead like this:

<HorizontalScrollView  android:layout_width="match_parent"  android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >           <!-- child views in here -->        //子控件在这儿    </LinearLayout></HorizontalScrollView>

现在你有了一个横向滑动的视图。

嵌套滑动视图

添加一个ScrollView到另外一个ScrollVIew会比较困难,大多数情况下并不好用,这里可以使用 NestedScrollView ,它和CoordiatorLayout一起使用时会比较好用

Adding a ScrollView within another ScrollView can be difficult. Most of the times it won’t end well. You will end up adding few workarounds. Instead, use the NestedScrollView as outlined here. A working sample can be found here as this is very useful when working with CoordinatorLayout

资源

•http://examples.javacodegeeks.com/android/core/ui/scrollview/android-scrollview-example/
•http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
•http://www.androidhub4you.com/2013/05/simple-scroll-view-example-in-android.html
•http://developer.android.com/reference/android/widget/ScrollView.html
•http://developer.android.com/reference/android/widget/HorizontalScrollView.html
•http://www.whitesof.com/?q=tutorials/android/android-scroll-view-tutorial

0 0