UI Interface

来源:互联网 发布:五金门知乎 编辑:程序博客网 时间:2024/05/08 01:36

view 是一个数据结构,他的属性存储了LAYOUT参数,内容是一片特定的矩形区域。

在这片矩形区域上他处理自己的MEASUREMENT,LAYOUT,DRAWING,FOCUS CHANGE,SCROLLING,KEY/GESTURE

 

Adapters

AdapterView是一个VIEWGOUP的实现,定义了基于一个ADAPTER对象的他的子VIEWS。

ADAPTER就像一个通讯员在你的数据源和ADAPTERVIEW之间。

 

Declaring Layout

每一个LAYOUT文件必须只有一个根元素,或者是VIEW,或者是VIEWGROUP

 

每一个VIEWGROUP都实现了自己的继承自ViewGroup.LayoutParams的特定类

比如一个LINEARLAYOUT包含了一个RELATIVELAYOUT和其他几个VIEW,RELATIVELAYOUT又包含了几个VIEW

那么定义RELATIVELAYOUT的LAYOUT参数应该用LINEARLAYOUT.LAYOUTPARAMS,而RELATIVELAYOUT中的

孩子应该用RELAVTIVELAYOUT.LAYOUTPARAMS.

 

一个VIEW实际有俩组宽高。

第一组是measured width and measured height这俩个尺寸定了它想在他的父亲里多大。The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().

第二组 width and height也被称作drawing width and drawing height, 这组定义了VIEW在屏幕上的实际尺寸。getWidth() and getHeight().

这俩组可能不同。

测量VIEW尺寸时,也要考虑他的PADDING,Padding can be used to offset the content of the view by a specific amount of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.

 

但是VIEW没有提供MARGIN,但是VIEWGROUP提供了。

 

Handling UI Events

ONKEYFrom View.OnKeyListener, 被调用当用户焦点在此VIEW上,按或者释放一个KEY

ONTOUCHFrom View.OnTouchListener,当按,释放,或者任何移动的手势动作在屏幕上。

onCreateContextMenu()From View.OnCreateContextMenuListener

This is called when a Context Menu is being built (as the result of a sustained "long click").

 

  • Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.
  • ViewGroup.onInterceptTouchEvent(MotionEvent) - This allows a ViewGroup to watch events as they are dispatched to child Views.
  • ViewParent.requestDisallowInterceptTouchEvent(boolean) - Call this upon a parent View to indicate that it should not intercept touch events with onInterceptTouchEvent(MotionEvent).

进入TOUCH模式后,只有 isFocusableInTouchMode() is true will be focusable, such as text editing widgets

其他的如BUTTON将不会被TAKE FOCUS。他们将简单的激活他们的ONCLICK。

 

TOUCH MODE 被整个系统(所有的WINDOWS,ACT)维护,为了查询当前状态call isInTouchMode() to see whether the device is currently in touch mode.

 

VIEW可以通过isFocusable()来指出他是否乐意接受焦点call setFocusable().区改变。

当在TOUCH MODE中allows focus with isFocusableInTouchMode(). You can change this with setFocusableInTouchMode().

 

如果VIEW不能获得FOUCS,你可以在申明时add the android:focusable XML attribute to the View, in your layout declaration. Set the value true.

You can also declare a View as focusable while in Touch Mode with android:focusableInTouchMode.

 

Building Custom Components

1.继承VIEW,填充构造函数,在这你能拿属性参数从XML,

几乎确定的是去覆盖ONMEASURE,可能需要覆盖ONDRAW,如果你想SHOW一些东西。

默认的ONDRAW什么也不干,而ONMEASURE将中给你一个尺寸为100*100

还有一些其他的的ON...需要覆盖的

 

你的VIEW的ONMEASURE应该计算 measurement width and height,这俩个尺寸将被用来呈现VIEW

尺寸应该在参数(widthMeasureSpec and heightMeasureSpec )规定之内,当然他也可以选择去超过他们

如果超过了,PARENT能选择区做一些事情,包括including clipping, scrolling, throwing an exception, or asking the onMeasure() to try again, perhaps with different measurement specifications

 

Once the width and height are calculated, the setMeasuredDimension(int width, int height) method must be called with the calculated measurements.

 

The custom View is defined in the LabelView class.

 

创建一个compound component

1.创建一个集成LAYOUT的类,以组合框为例,我可以用一个LINEARLAYOUT带着水平方向的

当然你也可以嵌套LAYOUT,做成任意复杂的部件,可以用申明XML的方式去创建包含的部件,或者代码

2.在构造函数里,先调用SUPER构造函数,然后你在这里可以创建EditText field and the PopupList

注意,你可能在XML引入了你自己的属性参数,能在这被弄出来

3.创建一些事件侦听为他所包含的VIEW

4.可以为你自己的属性创建一些访问器。比如允许EDITETEXT值被设置。

5,如果继承了一个LAYOUT,你不需要覆盖onDraw() and onMeasure()因为他们本身默认行为足够。也可以覆盖

The corresponding classes in the sample code are List4.java and List6.java.

 

public static class MyEditText extends EditText

This is a cleaner way to create inner classes if they do not need access to state from the outer class, keeps the generated class small, and allows it to be used easily from other classes

(自定义的VIEW如果inflated from an XML layout file,那么应该是一个带参数的构造器(Context context, AttributeSet attrs)被调用

在你的构造函数里要记住先调用SUPER)

怎么用定制的VIEW呢,这里有一个例子

<view
class="com.android.notepad.NoteEditor$MyEditText"
id="@+id/note"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:drawable/empty"
android:padding="10dip"
android:scrollbars="vertical"
android:fadingEdge="vertical" />

How Android Draws Views

当一个ACT接受到焦点时,将从这个LAYOUT的树层次的根节点开始画

沿着树,从上到下,每一个GROUP都负责请求他的孩子去画他自己(通过DRAW)

父亲总是最先画,然后是兄弟。

 

画LAYOUT是分俩个过程:MEASURE,LAYOUT

MEASURE过程也是一个自顶向下的过程implemented in measure(int, int)

每一个VIEW推动尺寸规范在这个递归过程中,在MEASURE结束时候,每一个VIEW存储了他的MEASUREMENTS。

LAYOUT也同样,自顶向下。在这个过程中,每一个PARENT负责定位他的孩子用在MEASURE过程计算得到的尺寸。

When a View's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that View's descendants

 

MEASURE过程用俩个类去通信和DIMENSIONS,

. The View.MeasureSpec class is used by Views to tell their parents how they want to be measured and positioned. The base LayoutParams class just describes how big the View wants to be for both width and height.

For each dimension, it can specify one of:

  • an exact number
  • FILL_PARENT, which means the View wants to be as big as its parent (minus padding)
  • WRAP_CONTENT, which means that the View wants to be just big enough to enclose its content (plus padding).

MeasureSpecs are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:

 

当ACT开始呈现他的内容时,首先从根节点VIEWGROUP开始

画VIEWGROUP就是用MEASURE,LAYOUT去测量定位他的子VIEW,这个过程完了

VIEWGROUP会被调用

dispatchDraw,然后轮次调用VIEW的DRAW。

VIEWGROUP:ONMEASURE-->ONLAYOUT-->DISPATCHDRAW-->  VIEW'S ONDRAW.

 

viewgroup设了一个标志位防止自己被画,因为LAYOUT本身不需要画东西,你也能设置这个标志位

所以继承iewgroup实现ONDRAW,没有用。

.

Usually, a ViewGroup calls measure() on its children from itsown onMeasure() method. Similarly, you must call the layout() method of thechildren from onLayout().

Note that for performance reasons, onMeasure() is not necessarily call everytime measure() is called. The view system tries to be smart and will invokeonMeasure() only when a View might have changed its bounds. Also, onLayout()is executed by layout() only when the bounds have changed. When onMeasure()is called on a View, onLayout() will also be called.

Last but not least, drawing is a bit different. At drawing time, ViewGroupwill enter the draw() method and instead of going through onDraw(), it willinvoke dispatchDraw(), which will in turn call draw() on every child.

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 8个月宝宝37.5度怎么办 8个月婴儿37.5度怎么办 5个月宝宝38度怎么办 5个月宝宝发烧怎么办 五个月小孩38度怎么办 宝宝3岁还不会说话 怎么办 两岁宝宝流口水厉害怎么办 两周宝宝不说话怎么办 两岁宝宝说话有点口吃怎么办 一周岁的宝宝脾气不好怎么办 九个月的宝宝脾气不好怎么办 两岁宝宝脾气不好怎么办 2岁宝宝脾气不好怎么办 3岁宝宝脾气不好怎么办 脾气不好吓到宝宝了怎么办 四个月宝宝脾气不好怎么办 5岁说话不清楚该怎么办 小孩快上幼儿园了不怎么说话怎么办 四岁宝宝吐字不清楚怎么办 孕28周胎儿腿短怎么办 b超显示腿短怎么办 2岁多宝宝不愿意说话怎么办 6岁儿童咬字不清怎么办 两岁宝宝爱看电视怎么办 两岁宝宝喜欢看电视怎么办 三岁宝宝说话不清晰怎么办 儿子快四岁了说话不清楚怎么办 儿子快三岁了说话不清楚怎么办 2岁宝宝受刺激了怎么办 一岁的宝宝口臭怎么办 古话说小孩牙齿掉了怎么办 宝宝三岁胆子小怎么办 小宝宝就是不愿意学说话怎么办 2岁宝宝体重偏重怎么办 三岁宝宝体重偏重怎么办 3岁宝宝说话晚怎么办 孩子吃饭总说话吃饭慢怎么办 我的孩子不聪明怎么办 小孩个子长不高怎么办 晚修自己老喜欢讲话怎么办 宝2岁宝脾气大怎么办