展会信息港展会大全

Android View中滚动 scrollTo scrollBy mScrollX mScrollY
来源:互联网   发布日期:2015-10-03 10:52:50   浏览:2396次  

导读:方法 * scrollTo: (内容的左上角)达到某个地点 * scrollBy: 根据当前位置,再移动多少属性: * mScrollX, 以下是文档解释 The offset, in pixels, by which the content of ...

方法

* scrollTo: (内容的左上角)达到某个地点

* scrollBy: 根据当前位置,再移动多少

属性:

*

mScrollX, 以下是文档解释

The offset, in pixels, by which the content of this view is scrolled horizontally.

*

mScrollY, 以下是文档解释

The offset, in pixels, by which the content of this view is scrolled vertically.

网上有很多blog翻译,就是简单的说是偏移量,但是没有具体的说相对哪个坐标的偏移量或者是哪个点的偏移量;

我的mScrollX/mScrollY的理解是:

当View水平滚动完成之后,此View内容的左边相对于View容器本身左边的偏移量;同理竖直方向也是;

当View竖直滚动完成之后,此View内容的上边相对于View容器本身上边的偏移量;

所有的滚动都是针对于View 里面的内容;

以下是例子

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:orientation="vertical"

android:layout_height="fill_parent" >

<HorizontalScrollView

android:background="#f888"

android:id="@+id/sv"

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

<LinearLayout

android:id="@+id/ll"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin" >

<Button

android:layout_width="300dp"

android:onClick="scrollX"

android:layout_height="wrap_content"

android:background="#ff00"

android:text="水平移动" />

<Button

android:onClick="scrollX"

android:layout_width="300dp"

android:layout_height="wrap_content"

android:background="#f0f0"

android:text="水平移动" />

<Button

android:onClick="scrollX"

android:layout_width="300dp"

android:layout_height="wrap_content"

android:background="#f00f"

android:text="水平移动" />

<Button

android:onClick="scrollX"

android:layout_width="300dp"

android:layout_height="wrap_content"

android:background="#ff0f"

android:text="水平移动" />

<Button

android:onClick="scrollX"

android:layout_width="300dp"

android:layout_height="wrap_content"

android:background="#fff0"

android:text="水平移动" />

<Button

android:onClick="scrollX"

android:layout_width="300dp"

android:layout_height="wrap_content"

android:background="#f0ff"

android:text="水平移动" />

</LinearLayout>

</HorizontalScrollView>

<ScrollView

android:id="@+id/sv2"

android:layout_width="fill_parent"

android:background="#fccc"

android:layout_height="wrap_content"

android:layout_weight="1" >

<LinearLayout

android:layout_width="match_parent"

android:layout_height="3600px"

android:orientation="vertical"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingBottom="1200px"

tools:context=".MainActivity" >

<Button

android:background="#ff00"

android:layout_width="wrap_content"

android:layout_height="300px"

android:onClick="scrollY"

android:text="垂直移动" />

<Button

android:background="#f0f0"

android:layout_width="wrap_content"

android:layout_height="300px"

android:onClick="scrollY"

android:text="垂直移动" />

<Button

android:background="#f00f"

android:layout_width="wrap_content"

android:layout_height="300px"

android:onClick="scrollY"

android:text="垂直移动" />

<Button

android:background="#f0ff"

android:layout_width="wrap_content"

android:layout_height="300px"

android:onClick="scrollY"

android:text="垂直移动" />

<Button

android:background="#ff0f"

android:layout_width="wrap_content"

android:layout_height="300px"

android:onClick="scrollY"

android:text="垂直移动" />

<Button

android:background="#fff0"

android:layout_width="wrap_content"

android:layout_height="300px"

android:onClick="scrollY"

android:text="垂直移动" />

</LinearLayout>

</ScrollView>

主要Activity代码

public voidscrollY(View view){

/**

*当scrollBy的Y为负:ScrollView中的内容向下移动

*当scrollBy的Y为正:....................................... 上移动

*/

svY.scrollBy(0, 300); // 内容向上走

System.out.println("YY: "+svY.getScrollY());

}

public voidscrollX(View view){

/**

* X为负: 右移动

* X为正:左移动

*/

svX.scrollBy(300, 0);// 内容向左走

System.out.println("XX: "+svX.getScrollX());

}

效果图

水平滚动图

http://static.oschina.net/uploads/space/2013/1230/155309_Fv4S_115541.gif

竖直滚动图

http://static.oschina.net/uploads/space/2013/1230/155844_DZrx_115541.gif

从效果和图片来看总结 scrollBy(x,y )

* 当x为正的时候,view中的内容往左边移动,负的时候往右边移动

* 但y为正的时候,view中的内容网上移动,负的时候往下移动

总结一句话:正负,上下,左右;

赞助本站

人工智能实验室

相关热词: scrollTo scrollBy mScrollX And

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港