Table of contents
1. Share state with parent layout
2. Set the View not to be displayed on system windows such as the status bar and navigation bar.
3. Focus can be set in touch mode
4. XML attributes of View class
5. Listener that can be set by the View class
I. Introduction
This article mainly records some basic View API usage and description in Android development.
2. View class
View
Classes represent the basic building blocks of user interface components. A View
square area that occupies the screen and is responsible for drawing or event processing in this area. The class is the base class for all components View
used to create interactiveUI
interfaces .
All windows View
are organized in a tree. Trees can be built by creating and assembling them in code View
or by writing one or more XML layout files . Concrete subclasses of can serve as controls or display text, images, or other content.View
View
Once the tree is created View
, there are several common types of operations that can be performed as needed:
- Set properties . For example setting the text property for a
TextView
. In addition toView
the defined properties and methods,View
different subclasses have some unique properties and methods. Note:View
Properties known when building the tree canXML
be set in the layout file. - Set focus .
Android
The system moves focus based on user input. UserequestFocus
methods to force focus to a specific view. 设置Listener
. AllowsView
settingslistener
to be notified when certain events of interest occur. For example, allView
can besetOnFocusChangeListener(android.view.View.OnFocusChangeListener)
set up to track whenView
focus is gained or lostlistener
. OtherView
subclasses are provided uniquelylistener
. For example, oneButton
can set up a function to monitor whether it is clickedlistener
.- Set up the visualization .
setVisibility(int)
It can be shown or hidden through methodsView
.
3. Usage records of some APIs
1. Share state with parent layout
This is an easily overlooked but very important attribute. Many people have never known this attribute, so they will adopt a custom touch method to achieve the click effect and touch effect of all sub-Views under a layout. In fact, it is not needed. You only need to add this attribute to let the following child views share the state after the parent layout is clicked or the focus is selected.
android:duplicateParentState="true"
2. Set the View not to be displayed on system windows such as the status bar and navigation bar.
<!-- 用于根据状态栏等系统窗口调整视图布局。如果为 true,则调整此视图的填充以为系统窗口留出空间。仅当此视图在非嵌入式活动中时才会生效 -->
<attr name="fitsSystemWindows" format="boolean" />
3. Focus can be set in touch mode
The focus here refers to the View selection focus when the remote control or keyboard is operated.
A Boolean value that controls whether the view can receive focus in touch mode. If this is true for one view, the view can gain focus when clicked, and can maintain focus if clicked on another view that does not have this property set to true.
android:focusableInTouchMode="true"
4. Set default focus
<!-- 此视图是否为默认焦点视图。每个键盘导航集群只有一个视图可以将此属性设置为 true。 -->
<attr name="focusedByDefault" format="boolean" />
5. Focus selection and highlight
<!-- 此视图是否应在获得焦点但未在其背景中定义时使用默认焦点突出显示. -->
<attr name="defaultFocusHighlightEnabled" format="boolean" />
6. Focus jump
If you are developing a TV application, you need to set the focus selected by the remote control. Generally, you do not need to use these focus jump positions, because the Android system will automatically retrieve the next focus that needs to jump. They are only used when we specifically require a certain jump focus position to be specified.
<!-- 当下一个焦点是时,定义下一个要关注的视图。如果引用引用的视图不存在或属于不可见的层次结构的一部分,则访问引用时将产生 {@link java.lang.RuntimeException}。-->
<attr name="nextFocusLeft" format="reference"/>
<attr name="nextFocusRight" format="reference"/>
<attr name="nextFocusUp" format="reference"/>
<attr name="nextFocusDown" format="reference"/>
<attr name="nextFocusForward" format="reference"/>
7. Display layer height
You can make View appear above or below other Views.
android:elevation="100dp"
8. Offset height
Same as the display layer height of "Item 7" above, which is just the offset.
android:translationZ="100dp"
9. Transparency
android:alpha="0.5"
10. Zoom factor
android:scaleX="1.5"
android:scaleY="1.5"
11. Offset movement position
android:translationY="100dp"
android:translationX="100dp"
12. Rotation angle
Set in xml file:
android:rotation="90"
android:rotationX="0.5"
android:rotationY="0.5"
Set in code:
setRotation(float类型数据);
setRotationX(float类型数据);
setRotationY(float类型数据);
13. View’s press sound effect
Set in xml file:
android:soundEffectsEnabled="true"
Set in code:
view.playSoundEffect(SoundEffectConstants.CLICK);
4. XML attributes of View class
Property name | Related methods | describe |
---|---|---|
android:accessibilityLiveRegion |
setAccessibilityLiveRegion(int) |
Instructs the accessibility service whether to notify the user when the View changes |
android:alpha |
setAlpha(float) |
View The transparency property of alpha , from 0 (fully transparent) to 1 (opaque) |
android:background |
setBackgroundResource(int) |
drawable an element that acts as a background |
android:clickable |
setClickable(boolean) |
Define View whether this responds to click events |
android:clickable |
setClickable(boolean) |
Define View whether this responds to click events |
android:contentDescription |
setContentDescription(CharSequence) |
Defines text that simply describes View the content, often used to provide accessibility support |
android:drawingCacheQuality |
setDrawingCacheQuality(int) |
Defines the quality of the transparent paint buffer |
android:duplicateParentState |
If this property is set to true, the View will obtain the drawing state (focus, pressed) from its direct parent class instead of itself. | |
android:fadeScrollbars |
setScrollbarFadingEnabled(boolean) |
Defines whether the scroll bar fades out when it is no longer useful |
android:fadingEdgeLength |
getVerticalFadingEdgeLength() |
Set the length of the upper and lower black shadow edges of the View |
android:filterTouchesWhenObscured |
setFilterTouchesWhenObscured(boolean) |
Specifies whether to filter touch events when the View's window is obscured by other visible windows. |
android:fitsSystemWindows |
setFitsSystemWindows(boolean) |
View Whether to adjust the layout according to the system window such as the status bar |
android:focusable |
setFocusable(boolean) |
Set whether the View can get focus |
android:focusableInTouchMode |
setFocusableInTouchMode(boolean) |
Sets whether the View can gain focus in touch mode |
android:hapticFeedbackEnabled |
setHapticFeedbackEnabled(boolean) |
Set View whether there is tactile feedback (support for long press and other events) |
android:id |
setId(int) |
Specify an identification name for this View , support the view.findViewById() or Activity.findViewById() method to obtain theView |
android:importantForAccessibility |
setImportantForAccessibility(int) |
Specifies View the importance of accessibility support for this |
android:isScrollContainer |
setScrollContainer(boolean) |
Specifies whether this View should be used as a scrollable container, allowing the entire window to be resized to shrink to make room for the input method. |
android:keepScreenOn |
setKeepScreenOn(boolean) |
Specifies whether View the window always keeps the screen on when it is visible |
android:layerType |
setLayerType(int,Paint) |
View Specifies the type of layer behind this (software or hardware layer) |
android:layoutDirection |
setLayoutDirection(int) |
Specify the direction in which the layout is drawn (left to right, right to left, etc.) |
android:longClickable |
setLongClickable(boolean) |
Specifies View whether to respond to long press events |
android:minHeight |
setMinimumHeight(int) |
Set View the minimum height of the |
android:minWidth |
setMinimumWidth(int) |
Set View the minimum width of the |
android:nextFocusDown |
setNextFocusDownId(int) |
当下一个焦点为FOCUS_DOWN 时,定义获得焦点的下一个View 。如果指定的View 不存在或不可见,则报RuntimeException 异常 |
android:nextFocusForward |
setNextFocusForwardId(int) |
当下一个焦点为FOCUS_FORWARD 时,定义获得焦点的下一个View 。如果指定的View 不存在或不可见,则报RuntimeException 异常 |
android:nextFocusLeft |
setNextFocusLeftId(int) |
当下一个焦点为FOCUS_LEFT 时,定义获得焦点的下一个View 。 |
android:nextFocusRight |
setNextFocusRightId(int) |
当下一个焦点为FOCUS_RIGHT 时,定义获得焦点的下一个View 。如果指定的View 不存在或不可见,则报RuntimeException 异常 |
android:nextFocusUp |
setNextFocusUpId(int) |
当下一个焦点为FOCUS_UP 时,定义获得焦点的下一个View 。如果指定的View 不存在或不可见,则报RuntimeException 异常 |
android:onClick |
当该View 被单击时触发的方法名称 |
|
android:padding |
setPaddingRelative(int,int,int,int) |
以像素值设置四条边的填充(View 的边界和内容之间的空间) |
android:paddingBottom |
setPaddingRelative(int,int,int,int) |
以像素值设置底边的填充 |
android:paddingEnd |
setPaddingRelative(int,int,int,int) |
以像素值设置右边的填充 |
android:paddingLeft |
setPadding(int,int,int,int) |
以像素值设置左边的填充 |
android:paddingRight |
setPadding(int,int,int,int) |
以像素值设置右边的填充 |
android:paddingStart |
setPaddingRelative(int,int,int,int) |
以像素值设置左边的填充 |
android:paddingTop |
setPaddingRelative(int,int,int,int) |
以像素值设置顶边的填充 |
android:requiresFadingEdge |
setVerticalFadingEdgeEnabled(boolean) |
定义在滚动时哪个边应该被有阴影 |
android:rotation |
setRotation(float) |
View 的旋转,以度为单位 |
android:rotationX |
setRotationX(float) |
View 沿x轴的旋转,以度为单位 |
android:rotationY |
setRotationY(float) |
View 沿y轴的旋转,以度为单位 |
android:saveEnabled |
setSaveEnabled(boolean) |
若为false,则冻结后该View 的所有状态都不会保存 |
android:scaleX |
setScaleX(float) |
View 沿x轴的缩放 |
android:scaleY |
setScaleY(float) |
View 沿y轴的缩放 |
android:scrollX |
初始水平滚动偏移量,单位像素 | |
android:scrollY |
初始竖直滚动偏移量,单位像素 | |
android:scrollbarAlwaysDrawHorizontalTrack |
指定水平滚动条是否一直显示 | |
android:scrollbarAlwaysDrawVerticalTrack |
指定竖直滚动条是否一直显示 | |
android:scrollbarDefaultDelayBeforeFade |
setScrollBarDefaultDelayBeforeFade(int) |
设置滚动条等待隐藏的时间,单位毫秒 |
android:scrollbarFadeDuration |
setScrollBarFadeDuration(int) |
设置滚动条隐藏的时间,单位毫秒 |
android:scrollbarSize |
setScrollBarSize(int) |
设置竖直滚动条的宽度和水平滚动条的高度 |
android:scrollbarStyle |
setScrollBarStyle(int) |
设置滚动条的样式和位置 |
android:scrollbarThumbHorizontal |
设置水平滚动条的Thumb 的drawable 元素 |
|
android:scrollbarThumbVertical |
设置竖直滚动条的Thumb 的drawable`元素 |
|
android:scrollbarTrackHorizontal |
设置水平滚动条的轨道的drawable 元素 |
|
android:scrollbarTrackVertical |
设置竖直滚动条的轨道的drawable 元素 |
|
android:scrollbars |
指定哪些滚动条应该显示 | |
android:soundEffectsEnabled |
setSoundEffectsEnabled(boolean) |
指定View 是否开启点击或触摸等特殊事件的声音效果 |
android:tag |
为该View 提供一个标记,通过View.getTag() 或View.findViewwithTag() 方法获得该View |
|
android:textAlignment |
setTextAlignment(int) |
定义文本的对齐 |
android:textDirection |
setTextDirection(int) |
定义文本的方向 |
android:transformPivotX |
setPivotX(float) |
旋转或缩放依赖的坐标轴点的x坐标 |
android:transformPivotY |
setPivotX(float) |
旋转或缩放依赖的坐标轴点的y坐标 |
android:translationX |
setTranslationX(float) |
该View 在x方向上的移动 |
android:translationY |
setTranslationY(float) |
The View movement in the y direction |
android:visibility |
setVisibility(int) |
Set the View initial visibility |
5. Listener that can be set by the View class
listener类 |
Setting method | describe |
---|---|---|
View.OnApplyWindowInsetsListener |
setOnApplyWindowInsetsListener |
Take over the View application Windows Insets policy |
View.OnClickListener |
setOnClickListener |
View The callback triggered when the button is clicked |
View.OnCreateContextMenuListener |
setOnCreateContextMenuListener |
View Callback triggered when the context menu is being constructed |
View.OnDragListener |
setOnDragListener |
View The callback triggered when the Drag event is received |
View.OnFocusChangeListener |
setOnFocusChangeListener |
View A callback triggered when the focus changes |
View.OnGenericMotionListener |
setOnGenericMotionListener |
View A callback triggered when a general movement event is received |
View.OnKeyListener |
setOnKeyListener |
View Callback triggered when the next hardware keyboard is pressed |
View.OnLongClickListener |
setOnLongClickListener |
View A callback triggered when the button is pressed and held for a long time |
View.OnSystemUiVisibilityChangeListener |
setOnSystemUiVisibilityChangeListener |
Callback triggered when the visibility of the system toolbar changes |
View.OnTouchListener |
setOnTouchListener |
A callback triggered when an event View is receivedtouch |