Android界面编程与视图组件——Android初探

Android应有的所有UI组件都继承了View类,View组件非常类似于Swing编程的JPanel,它代表一个空白的矩形区域。

View类还有一个重要的子类:ViewGroup,但它通常作为其他组件的容器使用。

Android的所有UI组件都是建立在View、ViewGroup基础之上的,Android采用了“组合器”设计模式来设计View和ViewGroup。对于Android图形用户界面来说,ViewGroup作为容器来盛装其他组件,而ViewGroup里除了包含View外,还可以再次包含ViewGroup。

 

View类的xml属性(官方链接--https://developer.android.google.cn/reference/kotlin/android/view/View?hl=en#xml-attributes)

android:accessibilityHeadingWhether or not this view is a heading for accessibility purposes.
android:alphaalpha property of the view, as a value between 0 (completely transparent) and 1 (completely opaque).
android:backgroundA drawable to use as the background.
android:clickableDefines whether this view reacts to click events.
android:contentDescriptionDefines text that briefly describes content of the view.
android:drawingCacheQualityDefines the quality of translucent drawing caches.
android:duplicateParentStateWhen this attribute is set to true, the view gets its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.
android:fadeScrollbarsDefines whether to fade out scrollbars when they are not in use.
android:fadingEdgeLengthDefines the length of the fading edges.
android:filterTouchesWhenObscuredSpecifies whether to filter touches when the view's window is obscured by another visible window.
android:fitsSystemWindowsBoolean internal attribute to adjust view layout based on system windows such as the status bar.
android:focusableControls whether a view can take focus.
android:focusableInTouchModeBoolean that controls whether a view can take focus while in touch mode.
android:focusedByDefaultWhether this view is a default-focus view.
android:hapticFeedbackEnabledBoolean that controls whether a view should have haptic feedback enabled for events such as long presses.
android:idSupply an identifier name for this view, to later retrieve it with View.
android:isScrollContainerSet this if the view will serve as a scrolling container, meaning that it can be resized to shrink its overall window so that there will be space for an input method.
android:keepScreenOnControls whether the view's window should keep the screen on while visible.
android:keyboardNavigationClusterWhether this view is a root of a keyboard navigation cluster.
android:layerTypeSpecifies the type of layer backing this view.
android:layoutDirectionDefines the direction of layout drawing.
android:longClickableDefines whether this view reacts to long click events.
android:minHeightDefines the minimum height of the view.
android:minWidthDefines the minimum width of the view.
android:nextClusterForwardDefines the next keyboard navigation cluster.
android:nextFocusDownDefines the next view to give focus to when the next focus is android.
android:nextFocusLeftDefines the next view to give focus to when the next focus is android.
android:nextFocusRightDefines the next view to give focus to when the next focus is android.
android:nextFocusUpDefines the next view to give focus to when the next focus is android.
android:onClickName of the method in this View's context to invoke when the view is clicked.
android:outlineAmbientShadowColorSets the color of the ambient shadow that is drawn when the view has a positive Z or elevation value.
android:outlineSpotShadowColorSets the color of the spot shadow that is drawn when the view has a positive Z or elevation value.
android:paddingSets the padding, in pixels, of all four edges.
android:paddingBottomSets the padding, in pixels, of the bottom edge; see android.
android:paddingEndSets the padding, in pixels, of the end edge; see android.
android:paddingHorizontalSets the padding, in pixels, of the left and right edges; see android.
android:paddingLeftSets the padding, in pixels, of the left edge; see android.
android:paddingRightSets the padding, in pixels, of the right edge; see android.
android:paddingStartSets the padding, in pixels, of the start edge; see android.
android:paddingTopSets the padding, in pixels, of the top edge; see android.
android:paddingVerticalSets the padding, in pixels, of the top and bottom edges; see android.
android:requiresFadingEdgeDefines which edges should be faded on scrolling.
android:rotationrotation of the view, in degrees.
android:rotationXrotation of the view around the x axis, in degrees.
android:rotationYrotation of the view around the y axis, in degrees.
android:saveEnabledIf false, no state will be saved for this view when it is being frozen.
android:scaleXscale of the view in the x direction.
android:scaleYscale of the view in the y direction.
android:scrollXThe initial horizontal scroll offset, in pixels.
android:scrollYThe initial vertical scroll offset, in pixels.
android:scrollbarAlwaysDrawHorizontalTrackDefines whether the horizontal scrollbar track should always be drawn.
android:scrollbarAlwaysDrawVerticalTrackDefines whether the vertical scrollbar track should always be drawn.
android:scrollbarDefaultDelayBeforeFadeDefines the delay in milliseconds that a scrollbar waits before fade out.
android:scrollbarFadeDurationDefines the delay in milliseconds that a scrollbar takes to fade out.
android:scrollbarSizeSets the width of vertical scrollbars and height of horizontal scrollbars.
android:scrollbarStyleControls the scrollbar style and position.
android:scrollbarThumbHorizontalDefines the horizontal scrollbar thumb drawable.
android:scrollbarThumbVerticalDefines the vertical scrollbar thumb drawable.
android:scrollbarTrackHorizontalDefines the horizontal scrollbar track drawable.
android:scrollbarTrackVerticalDefines the vertical scrollbar track drawable.
android:scrollbarsDefines which scrollbars should be displayed on scrolling or not.
android:soundEffectsEnabledBoolean that controls whether a view should have sound effects enabled for events such as clicking and touching.
android:stateListAnimatorSets the state-based animator for the View.
android:tagSupply a tag for this view containing a String, to be retrieved later with View.
android:textAlignmentDefines the alignment of the text.
android:textDirectionDefines the direction of the text.
android:themeSpecifies a theme override for a view.
android:transformPivotXx location of the pivot point around which the view will rotate and scale.
android:transformPivotYy location of the pivot point around which the view will rotate and scale.
android:transitionNameNames a View such that it can be identified for Transitions.
android:translationXtranslation in x of the view.
android:translationYtranslation in y of the view.
android:translationZtranslation in z of the view.
android:visibilityControls the initial visibility of the view.

ViewGroup继承了View,也可以使用这些属性,但它主要还是当做容器类使用。由于它是一个抽象类,实际使用时通常使用它的子类来作为容器,例如各种布局管理器。

ViewGroup控制器控制其子组件的分布依赖于ViewGroup.LayoutParms、ViewGroup.MarginLayoutParams两个内部类,它们都提供了一些xml属性。

在Android Studio中,进行UI界面的编程,可以通过以下几种方式进行设计:

1、使用XML布局文件控制UI界面

在AS中,应用的布局文件资源在/res/layout目录下,需要新增一个布局文件时,只需在该目录下新建一个xml文件,然后通过setContentView(R.id.<资源文件名字>)即可显示相应的视图。

2、在代码中控制UI界面(此处不做细讲,待回头有时间会加上)

3、使用XML和代码混合控制UI界面

 

开发自定义View

当Android系统提供的UI组件不足以满足项目需求时,基于Android组件的实现原理,我们完全可以通过继承View来派生自定义组件,从而开发出项目定制的组件。

首先我们需要定义一个继承View基类的子类,然后重写View的一个或者多个方法(当自定义View时,不一定需要重写其中所有方法)。

通常可以被用户重写的方法有:(下面方法中相关形参已省略)

  • 构造器:重写构造器是定制View的最基本方式,创建该类的实例或者根据XML布局文件加载并构建界面时,需要调用该构造器。
  • onFinishInflate():回调方法,当应用从xml布局文件加载该组件并利用它来构造界面后,该方法会被回调。
  • onMeasure():检测View组件及其包含子组件的位置、大小时,被回调
  • onLayout():当该组件需要分配其子组件的位置、大小时,被回调
  • onSizeChanged():当组件大小被改变时,被回调
  • onDraw():将要绘制组件内容时,被回调
  • onKeyDown():某个键被按下时,触发该方法
  • onKeyUp():松开某个键时,触发该方法
  • OnTrackballEvent():当发生轨迹球事件时触发该方法
  • onTouchEvent():当发生触摸屏事件时触发该方法
  • onFoucusChanged():当组件焦点发生改变时,触发该方法
  • onWindowFocusChanged():当包含该组件窗口的可见性发生改变时触发该方法
  • onAttacheedToWindow():当把组件放入某个窗口中时,触发该方法
  • onDetachedFromWindow():当把组件从某个窗口中分离时,触发该方法
  • onWindowVisibilityChanged():当组件的窗口可见性发生改变时,触发该方法

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值