Most visited

Recently visited

Added in API level 1
Deprecated since API level 16

Gallery

public class Gallery
extends AbsSpinner implements GestureDetector.OnGestureListener

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.AdapterView<android.widget.SpinnerAdapter>
         ↳ android.widget.AbsSpinner
           ↳ android.widget.Gallery


此类已在API级别16中弃用。
此小部件不再受支持。 其他水平滚动窗口小部件包括来自支持库的HorizontalScrollViewViewPager

显示中心锁定水平滚动列表中的项目的视图。

Gallery的默认值假定您将使用Theme_galleryItemBackground作为从适配器提供给Gallery的每个View的背景。 如果您没有这样做,您可能需要调整某些图库属性,例如间距。

给予Gallery的视图应该使用 Gallery.LayoutParams作为它们的布局参数类型。

Summary

Nested classes

class Gallery.LayoutParams

Gallery扩展了LayoutParams以提供一个位置来保存当前转换信息以及之前的位置/转换信息。

XML attributes

android:animationDuration Sets how long a transition animation should run (in milliseconds) when layout has changed. 
android:gravity Specifies how an object should position its content, on both the X and Y axes, within its own bounds. 
android:spacing  
android:unselectedAlpha Sets the alpha on the items that are not selected. 

Inherited XML attributes

From class android.widget.AbsSpinner
From class android.view.ViewGroup
From class android.view.View

Inherited constants

From class android.widget.AdapterView
From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

Gallery(Context context)
Gallery(Context context, AttributeSet attrs)
Gallery(Context context, AttributeSet attrs, int defStyleAttr)
Gallery(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

boolean dispatchKeyEvent(KeyEvent event)

将关键事件分派到焦点路径上的下一个视图。

void dispatchSetSelected(boolean selected)

调度setSelected选择到所有这个View的孩子。

ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)

根据提供的属性集返回一组新的布局参数。

CharSequence getAccessibilityClassName()

返回此对象的类名称以用于辅助功能。

boolean onDown(MotionEvent e)

当触发触发它的向下触发 MotionEvent时发出通知。

boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)

当它发生在初始状态为 MotionEvent和匹配状态为 MotionEvent时,通知其发生了 MotionEvent

boolean onKeyDown(int keyCode, KeyEvent event)

处理左侧,右侧和点击

boolean onKeyUp(int keyCode, KeyEvent event)

的默认实现 KeyEvent.Callback.onKeyUp() :当视图进行点击 KEYCODE_DPAD_CENTERKEYCODE_ENTER或者 KEYCODE_SPACE被释放。

void onLongPress(MotionEvent e)

当发生长 MotionEvent ,通过触发 MotionEvent的初始状态 MotionEvent

boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)

当滚动发生时通知通知 MotionEvent和当前移动 MotionEvent

void onShowPress(MotionEvent e)

用户已经执行了下跌 MotionEvent并且没有执行任何操作。

boolean onSingleTapUp(MotionEvent e)

当点击发生时触发它的通知 MotionEvent

boolean onTouchEvent(MotionEvent event)

实现此方法来处理触摸屏幕动作事件。

void setAnimationDuration(int animationDurationMillis)

设置在子视图更改位置时应该运行过渡动画的时间。

void setCallbackDuringFling(boolean shouldCallback)

是否在物品被 getOnItemSelectedListener()时回调任何getOnItemSelectedListener()

void setGravity(int gravity)

介绍子视图如何对齐。

void setSpacing(int spacing)

设置图库中项目之间的间距

void setUnselectedAlpha(float unselectedAlpha)

设置图库中未选择的项目的Alpha。

boolean showContextMenu()

显示该视图的上下文菜单。

boolean showContextMenu(float x, float y)

显示锚定到指定视图相对坐标的此视图的上下文菜单。

boolean showContextMenuForChild(View originalView)

显示指定视图或其祖先的上下文菜单。

boolean showContextMenuForChild(View originalView, float x, float y)

显示指定视图或其祖先的上下文菜单,该上下文菜单被锚定到指定的视图相对坐标。

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
int computeHorizontalScrollExtent()

计算水平滚动条拇指在水平范围内的水平范围。

int computeHorizontalScrollOffset()

计算水平滚动条拇指在水平范围内的水平偏移量。

int computeHorizontalScrollRange()

计算水平滚动条代表的水平范围。

void dispatchSetPressed(boolean pressed)

派发setPressed给所有这个View的孩子。

ViewGroup.LayoutParams generateDefaultLayoutParams()

返回一组默认布局参数。

ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)

根据提供的布局参数返回一组安全的布局参数。

int getChildDrawingOrder(int childCount, int i)

返回为此迭代绘制的子的索引。

boolean getChildStaticTransformation(View child, Transformation t)

t设置为子级的静态转换,如果设置,则返回布尔值以指示是否设置了静态转换。

ContextMenu.ContextMenuInfo getContextMenuInfo()

视图应该实现这个,如果他们有额外的信息与上下文菜单相关联。

void onAttachedToWindow()

这在视图附加到窗口时被调用。

void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)

当视图的焦点状态改变时,由视图系统调用。

void onLayout(boolean changed, int l, int t, int r, int b)

当这个视图为每个孩子分配一个大小和位置时,从布局调用。

Inherited methods

From class android.widget.AbsSpinner
From class android.widget.AdapterView
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource
From interface android.view.GestureDetector.OnGestureListener

XML attributes

android:animationDuration

设置布局更改时应该运行过渡动画的时间(以毫秒为单位)。 仅在开启动画时才有关。

必须是整数值,例如“ 100 ”。

这也可能是对包含此类型值的资源(形式为“ @[package:]type:name ”)或主题属性(形式为“ ?[package:][type:]name ”)的 ?[package:][type:]name

这对应于全局属性资源符号 animationDuration

相关方法:

android:gravity

指定对象应如何在其X轴和Y轴上将其内容定位在其自己的范围内。

必须是以下常量值中的一个或多个(用'|'分隔)。

Constant Value 描述
top 0x30 Push object to the top of its container, not changing its size.
bottom 0x50 Push object to the bottom of its container, not changing its size.
left 0x03 Push object to the left of its container, not changing its size.
right 0x05 Push object to the right of its container, not changing its size.
center_vertical 0x10 Place object in the vertical center of its container, not changing its size.
fill_vertical 0x70 Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal 0x01 Place object in the horizontal center of its container, not changing its size.
fill_horizontal 0x07 Grow the horizontal size of the object if needed so it completely fills its container.
center 0x11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill 0x77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
clip_vertical 0x80 Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
clip_horizontal 0x08 Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
start 0x00800003 Push object to the beginning of its container, not changing its size.
end 0x00800005 Push object to the end of its container, not changing its size.

这对应于全局属性资源符号 gravity

相关方法:

android:spacing

相关方法:

android:unselectedAlpha

设置未选择的项目上的字母。

必须是浮点值,例如“ 1.2 ”。

这也可能是对包含此类型值的资源(形式为“ @[package:]type:name ”)或主题属性(形式为“ ?[package:][type:]name ”)的 ?[package:][type:]name

这对应于全局属性资源符号 unselectedAlpha

相关方法:

Public constructors

Gallery

Added in API level 1
Gallery (Context context)

Parameters
context Context

Gallery

Added in API level 1
Gallery (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

Gallery

Added in API level 1
Gallery (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context
attrs AttributeSet
defStyleAttr int

Gallery

Added in API level 21
Gallery (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context
attrs AttributeSet
defStyleAttr int
defStyleRes int

Public methods

dispatchKeyEvent

Added in API level 1
boolean dispatchKeyEvent (KeyEvent event)

将关键事件分派到焦点路径上的下一个视图。 此路径从视图树的顶部向下延伸到当前聚焦的视图。 如果这个观点有重点,它会发送给自己。 否则,它将沿着焦点路径调度下一个节点。 这个方法也会触发任何关键的监听器。

Parameters
event KeyEvent: The key event to be dispatched.
Returns
boolean True if the event was handled, false otherwise.

dispatchSetSelected

Added in API level 1
void dispatchSetSelected (boolean selected)

调度setSelected选择到所有这个View的孩子。

Parameters
selected boolean: The new selected state

generateLayoutParams

Added in API level 1
ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

根据提供的属性集返回一组新的布局参数。

Parameters
attrs AttributeSet: the attributes to build the layout parameters from
Returns
ViewGroup.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

getAccessibilityClassName

Added in API level 23
CharSequence getAccessibilityClassName ()

返回此对象的类名称以用于辅助功能。 如果子类正在实现的东西应该被视为一个全新的视图类,当它被可访问性使用时,子类只应该覆盖这个子类,与它所源自的类无关。 这用于填写AccessibilityNodeInfo.setClassName

Returns
CharSequence

onDown

Added in API level 1
boolean onDown (MotionEvent e)

当触发触发它的向下触发MotionEvent触发通知。 这将在每次下降事件时立即触发。 所有其他事件应该在此之前。

Parameters
e MotionEvent: The down motion event.
Returns
boolean

onFling

Added in API level 1
boolean onFling (MotionEvent e1, 
                MotionEvent e2, 
                float velocityX, 
                float velocityY)

当它发生在MotionEvent和匹配MotionEvent的初始状态时,通知其发生了MotionEvent 计算出的速度沿x和y轴以像素/秒为单位提供。

Parameters
e1 MotionEvent: The first down motion event that started the fling.
e2 MotionEvent: The move motion event that triggered the current onFling.
velocityX float: The velocity of this fling measured in pixels per second along the x axis.
velocityY float: The velocity of this fling measured in pixels per second along the y axis.
Returns
boolean true if the event is consumed, else false

onKeyDown

Added in API level 1
boolean onKeyDown (int keyCode, 
                KeyEvent event)

处理左侧,右侧和点击

Parameters
keyCode int: a key code that represents the button pressed, from KeyEvent
event KeyEvent: the KeyEvent object that defines the button action
Returns
boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

也可以看看:

onKeyUp

Added in API level 1
boolean onKeyUp (int keyCode, 
                KeyEvent event)

的默认实现 KeyEvent.Callback.onKeyUp() :当视图进行点击 KEYCODE_DPAD_CENTERKEYCODE_ENTER或者 KEYCODE_SPACE被释放。

软件键盘中的按键通常不会触发这个监听器,尽管在某些情况下有些人会选择这样做。 不要依靠这个来捕捉软件按键。

Parameters
keyCode int: A key code that represents the button pressed, from KeyEvent.
event KeyEvent: The KeyEvent object that defines the button action.
Returns
boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

onLongPress

Added in API level 1
void onLongPress (MotionEvent e)

当发生长 MotionEvent ,通过触发 MotionEvent的初始触发通知。

Parameters
e MotionEvent: The initial on down motion event that started the longpress.

onScroll

Added in API level 1
boolean onScroll (MotionEvent e1, 
                MotionEvent e2, 
                float distanceX, 
                float distanceY)

当滚动发生时,通知通知的初始时间为MotionEvent ,当前时间为MotionEvent x和y的距离也是为了方便而提供的。

Parameters
e1 MotionEvent: The first down motion event that started the scrolling.
e2 MotionEvent: The move motion event that triggered the current onScroll.
distanceX float: The distance along the X axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.
distanceY float: The distance along the Y axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.
Returns
boolean true if the event is consumed, else false

onShowPress

Added in API level 1
void onShowPress (MotionEvent e)

用户已经执行了下跌MotionEvent并且没有执行任何操作。 此事件通常用于向用户提供视觉反馈,让他们知道他们的行为已被识别,即突出显示一个元素。

Parameters
e MotionEvent: The down motion event

onSingleTapUp

Added in API level 1
boolean onSingleTapUp (MotionEvent e)

当点击发生时触发它的通知通知 MotionEvent

Parameters
e MotionEvent: The up motion event that completed the first tap
Returns
boolean true if the event is consumed, else false

onTouchEvent

Added in API level 1
boolean onTouchEvent (MotionEvent event)

实现此方法来处理触摸屏幕动作事件。

如果此方法用于检测点击操作,建议通过执行并调用performClick()来执行操作。 这将确保一致的系统行为,包括:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
event MotionEvent: The motion event.
Returns
boolean True if the event was handled, false otherwise.

setAnimationDuration

Added in API level 1
void setAnimationDuration (int animationDurationMillis)

设置在子视图更改位置时应该运行过渡动画的时间。 仅在开启动画时才有关。

相关XML属性:

Parameters
animationDurationMillis int: The duration of the transition, in milliseconds.

setCallbackDuringFling

Added in API level 1
void setCallbackDuringFling (boolean shouldCallback)

当物品被getOnItemSelectedListener()时是否回调任何getOnItemSelectedListener() 如果为false,则只有最终选定的项目会导致回调。 如果为true,则第一个和最后一个之间的所有项都将导致回调。

Parameters
shouldCallback boolean: Whether or not to callback on the listener while the items are being flinged.

setGravity

Added in API level 1
void setGravity (int gravity)

介绍子视图如何对齐。

相关XML属性:

setSpacing

Added in API level 1
void setSpacing (int spacing)

设置图库中项目之间的间距

相关XML属性:

Parameters
spacing int: The spacing in pixels between items in the Gallery

setUnselectedAlpha

Added in API level 1
void setUnselectedAlpha (float unselectedAlpha)

设置图库中未选择的项目的Alpha。

相关XML属性:

Parameters
unselectedAlpha float: the alpha for the items that are not selected.

showContextMenu

Added in API level 1
boolean showContextMenu ()

显示该视图的上下文菜单。

Returns
boolean true if the context menu was shown, false otherwise

showContextMenu

Added in API level 24
boolean showContextMenu (float x, 
                float y)

显示锚定到指定视图相对坐标的此视图的上下文菜单。

Parameters
x float: the X coordinate in pixels relative to the view to which the menu should be anchored, or NaN to disable anchoring
y float: the Y coordinate in pixels relative to the view to which the menu should be anchored, or NaN to disable anchoring
Returns
boolean true if the context menu was shown, false otherwise

showContextMenuForChild

Added in API level 1
boolean showContextMenuForChild (View originalView)

显示指定视图或其祖先的上下文菜单。

在大多数情况下,子类不需要覆盖它。 但是,如果子类直接添加到窗口管理器(例如, addView(View, android.view.ViewGroup.LayoutParams) ),则应该覆盖此并显示上下文菜单。

Parameters
originalView View: the source view where the context menu was first invoked
Returns
boolean true if the context menu was shown, false otherwise

showContextMenuForChild

Added in API level 24
boolean showContextMenuForChild (View originalView, 
                float x, 
                float y)

显示指定视图或其祖先的上下文菜单,该上下文菜单被锚定到指定的视图相对坐标。

在大多数情况下,子类不需要覆盖它。 但是,如果子类直接添加到窗口管理器(例如, addView(View, android.view.ViewGroup.LayoutParams) ),则应该覆盖此并显示上下文菜单。

如果一个子类覆盖了这个方法,它也应该覆盖 showContextMenuForChild(View)

Parameters
originalView View: the source view where the context menu was first invoked
x float: the X coordinate in pixels relative to the original view to which the menu should be anchored, or NaN to disable anchoring
y float: the Y coordinate in pixels relative to the original view to which the menu should be anchored, or NaN to disable anchoring
Returns
boolean true if the context menu was shown, false otherwise

Protected methods

checkLayoutParams

Added in API level 1
boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams
Returns
boolean

computeHorizontalScrollExtent

Added in API level 1
int computeHorizontalScrollExtent ()

计算水平滚动条拇指在水平范围内的水平范围。 该值用于计算滚动条轨道内的拇指长度。

范围以任意单位表示,必须与 computeHorizontalScrollRange()computeHorizontalScrollOffset()使用的单位相同。

默认范围是此视图的图纸宽度。

Returns
int the horizontal extent of the scrollbar's thumb

computeHorizontalScrollOffset

Added in API level 1
int computeHorizontalScrollOffset ()

计算水平滚动条拇指在水平范围内的水平偏移量。 该值用于计算滚动条轨道内的拇指位置。

范围以任意单位表示,必须与 computeHorizontalScrollRange()computeHorizontalScrollExtent()使用的单位相同。

默认偏移量是该视图的滚动偏移量。

Returns
int the horizontal offset of the scrollbar's thumb

computeHorizontalScrollRange

Added in API level 1
int computeHorizontalScrollRange ()

计算水平滚动条代表的水平范围。

范围以任意单位表示,必须与 computeHorizontalScrollExtent()computeHorizontalScrollOffset()使用的单位相同。

默认范围是此视图的图纸宽度。

Returns
int the total horizontal range represented by the horizontal scrollbar

dispatchSetPressed

Added in API level 1
void dispatchSetPressed (boolean pressed)

派发setPressed给所有这个View的孩子。

Parameters
pressed boolean: The new pressed state

generateDefaultLayoutParams

Added in API level 1
ViewGroup.LayoutParams generateDefaultLayoutParams ()

返回一组默认布局参数。 当传递给addView(View)的视图没有设置布局参数时,请求这些参数。 如果返回null,则会从addView引发异常。

Returns
ViewGroup.LayoutParams a set of default layout parameters or null

generateLayoutParams

Added in API level 1
ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

根据提供的布局参数返回一组安全的布局参数。 当一个ViewGroup被传递了一个View,其布局参数不能通过checkLayoutParams(android.view.ViewGroup.LayoutParams)的测试时,这个方法被调用。 此方法应该返回一组适合此ViewGroup的布局参数,可能是通过从指定的一组布局参数中复制适当的属性。

Parameters
p ViewGroup.LayoutParams: The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Returns
ViewGroup.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

getChildDrawingOrder

Added in API level 1
int getChildDrawingOrder (int childCount, 
                int i)

返回为此迭代绘制的子的索引。 如果要更改儿童的绘图顺序,请覆盖此选项。 默认情况下,它返回i。

注:为了调用此方法,您必须先致电 setChildrenDrawingOrderEnabled(boolean)以启用子订单。

Parameters
childCount int
i int: The current iteration.
Returns
int The index of the child to draw this iteration.

getChildStaticTransformation

Added in API level 1
boolean getChildStaticTransformation (View child, 
                Transformation t)

t设置为子级的静态转换,如果设置,则返回布尔值以指示是否设置了静态转换。 默认实现简单地返回false ; 子类可以针对不同的行为重写此方法。 要调用此方法,必须将setStaticTransformationsEnabled(boolean)设置为true。

Parameters
child View: The child view whose static transform is being requested
t Transformation: The Transformation which will hold the result
Returns
boolean true if the transformation was set, false otherwise

getContextMenuInfo

Added in API level 1
ContextMenu.ContextMenuInfo getContextMenuInfo ()

视图应该实现这个,如果他们有额外的信息与上下文菜单相关联。 返回结果作为参数提供给onCreateContextMenu(ContextMenu, View, ContextMenuInfo)回调。

Returns
ContextMenu.ContextMenuInfo Extra information about the item for which the context menu should be shown. This information will vary across different subclasses of View.

onAttachedToWindow

Added in API level 1
void onAttachedToWindow ()

这在视图附加到窗口时被调用。 此时它有一个Surface并将开始绘制。 请注意,此功能保证在onDraw(android.graphics.Canvas)之前onDraw(android.graphics.Canvas) ,但可以在第一次onDraw之前的任何时候调用此函数 - 包括onMeasure(int, int)之前或之后。

onFocusChanged

Added in API level 1
void onFocusChanged (boolean gainFocus, 
                int direction, 
                Rect previouslyFocusedRect)

当视图的焦点状态改变时,由视图系统调用。 当焦点更改事件是由方向导航导致的,direction和previouslyFocusedRect提供了焦点来自何处的洞察。 重写时,一定要调用超类,以便进行标准的焦点处理。

Parameters
gainFocus boolean: True if the View has focus; false otherwise.
direction int: The direction focus has moved when requestFocus() is called to give this view focus. Values are FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD, or FOCUS_BACKWARD. It may not always apply, in which case use the default.
previouslyFocusedRect Rect: The rectangle, in this view's coordinate system, of the previously focused view. If applicable, this will be passed in as finer grained information about where the focus is coming from (in addition to direction). Will be null otherwise.

onLayout

Added in API level 1
void onLayout (boolean changed, 
                int l, 
                int t, 
                int r, 
                int b)

当这个视图为每个孩子分配一个大小和位置时,从布局调用。 带孩子的派生类应该覆盖这个方法,并调用他们每个孩子的布局。

Parameters
changed boolean: This is a new size or position for this view
l int: Left position, relative to parent
t int: Top position, relative to parent
r int: Right position, relative to parent
b int: Bottom position, relative to parent

Hooray!