view和viewGroup
->view,視圖是屏幕上可見的矩形區(qū)域宽气。視圖具有寬度和高度氏淑,有時還具有背景色贰拿。 插圖顯示了三種不同類型的視圖已脓。ImageView 顯示圖像搓谆,如圖標(biāo)或照片拆内。TextView 顯示文本栏尚。Button 是對觸摸敏感的 TextView:用手指點擊時即會做出響應(yīng)。ViewGroup 是大視圖拥峦,通常不可見贴膘,其內(nèi)部 包含并可放置較小視圖。
->viewGroup略号,ViewGroup 是大視圖刑峡,其中可包含小視圖。小視圖稱為 ViewGroup 的子視圖玄柠,可以是 TextView 或 ImageView突梦。ViewGroup 稱為其子視圖的父視圖。插圖顯示了最常見的 ViewGroup 之一羽利,即垂直的 LinearLayout宫患。
->View是所有UI組件的基類,而 ViewGroup是容納這些組件的容器这弧,其本身也是從View派生出來的.
->一般來說娃闲,開發(fā)Android應(yīng)用程序的UI界面都不會直接使用View和ViewGroup虚汛,而是使用這兩大基類的派生類。
View派生出的直接子類有:AnalogClock,ImageView,KeyboardView, ProgressBar,SurfaceView,TextView,ViewGroup,ViewStub
View派生出的間接子類有:AbsListView,AbsSeekBar, AbsSpinner, AbsoluteLayout, AdapterView<T extends Adapter>,AdapterViewAnimator, AdapterViewFlipper, AppWidgetHostView, AutoCompleteTextView,Button,CalendarView, CheckBox, CheckedTextView, Chronometer, CompoundButton,
ViewGroup派生出的直接子類有:AbsoluteLayout,AdapterView<T extends Adapter>,FragmentBreadCrumbs,FrameLayout,LinearLayout,RelativeLayout,SlidingDrawer
ViewGroup派生出的間接子類有:AbsListView,AbsSpinner, AdapterViewAnimator, AdapterViewFlipper, AppWidgetHostView, CalendarView, DatePicker, DialerFilter, ExpandableListView, Gallery, GestureOverlayView,GridView,HorizontalScrollView, ImageSwitcher,ListView,
->View和ViewGroup最重要的幾個方法
->protected void onDraw(Canvas canvas)皇帮,View類中用于重繪的方法泽疆,這個方法是所有View、ViewGroup及其派生類都具有的方法玲献,也是Android UI繪制最重要的方法。開發(fā)者可重載該方法梯浪,并在重載的方法內(nèi)部基于參數(shù)canvas繪制自己的各種圖形捌年、圖像效果;
->protected void onLayout(boolean changed, int left, int top, int right, int bottom)挂洛,View類中布局發(fā)生改變時會調(diào)用的方法礼预,這個方法是所有View、ViewGroup及其派生類都具有的方法虏劲,重載該類可以在布局發(fā)生改變時作定制處理托酸,這在實現(xiàn)一些特效時非常有用;
->protected void dispatchDraw(Canvas canvas)柒巫,ViewGroup類及其派生類具有的方法励堡,這個方法主要用于控制子View的繪制分發(fā),重載該方法可改變子View的繪制堡掏,進(jìn)而實現(xiàn)一些復(fù)雜的視效应结;
->protected boolean drawChild(Canvas canvas, View child, long drawingTime)),iewGroup類及其派生類具有的方法泉唁,這個方法直接控制繪制某局具體的子view鹅龄,重載該方法可控制具體某個具體子View。
view 的各個listener監(jiān)聽器亭畜?
->1扮休,在Activity中定義一個內(nèi)部類繼承監(jiān)聽器接口;
class MyListener implements View.OnClickListener{
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"you have clicked Button2",Toast.LENGTH_SHORT).show();
}
}
// 或者,這里是創(chuàng)建一個OnClickListener 的對象拴鸵,與上面的直接復(fù)寫接口有異曲同工之妙
private View.OnClickListener MyListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"you have clicked Button2",Toast.LENGTH_SHORT).show();
}
};
->2玷坠,實現(xiàn)匿名內(nèi)部類。這種方法適合只希望對監(jiān)聽器進(jìn)行一次性使用的情況宝踪,在該代碼塊運行完畢之后侨糟,該監(jiān)聽器也就不復(fù)存在了。
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"you have clicked Button1",Toast.LENGTH_SHORT).show();
}
});
->3瘩燥,利用布局文件中的onClick屬性秕重,并在實現(xiàn)文件中實現(xiàn)該方法。注意的是這里的方法名應(yīng)該和布局文件中onClick屬性的方法名相同厉膀,該方法必須是public方法溶耘。
// 方法三二拐,注意需要public方法
public void onButtonClick (View view){
Toast.makeText(MainActivity.this,"you have clicked Button3",Toast.LENGTH_SHORT).show();
}
}
<Button
android:layout_below="@id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="Button3"
android:onClick="onButtonClick"/>
view的構(gòu)造函數(shù)參數(shù)的意義
->構(gòu)造函數(shù)參數(shù)最多有四個:
- View(Context)
- View(Context, AttributeSet)
- View(Context, AttributeSet, defStyleAttr)
- View(Context, AttributeSet, defStyleAttr, defStyleRes)
Context - View中隨處都會用到;
AttributeSet - XML屬性(當(dāng)從XML inflate的時候)凳兵;
int defStyleAttr - 應(yīng)用到View的默認(rèn)風(fēng)格(定義在主題中)百新;
int defStyleResource - 如果沒有使用defStyleAttr,應(yīng)用到View的默認(rèn)風(fēng)格庐扫; 除了Context饭望,其它的參數(shù)只是用來通過XML屬性配置View的初始狀態(tài)(從布局,style以及theme中)形庭。
一般這樣設(shè)置我的自定義View: - SomeView(Context context) {
- this(context, null);
- }
- SomeView(Context context, AttributeSet attrs) {
- // Call super() so that the View sets itself up properly
- super(context, attrs);
- // ...Setup View and handle all attributes here...
- }
只需要這個兩個參數(shù)的構(gòu)造方法你就能隨意的使用obtainStyledAttributes()了铅辞。實現(xiàn)默認(rèn)樣式的一個簡便方法是直接提供defStyleRes給它。
viewGroup 怎么管理子view?
->1萨醒,ViewGroup 初始化計算width斟珊,height;
->2富纸,onMeasure獲得ViewGroup本身的width囤踩,height,可以重新去定義width晓褪,height堵漱,同時,通過 getChildCount可以獲得childview的個數(shù) 涣仿,如果是動態(tài)加載childView怔锌,需要在這里計算所有的childView的width, height变过;
->3埃元,onMeasure之后,ViewGroup的size就確定了媚狰, 可以通過onSizeChanged獲得ViewGroup真實有效的width和height岛杀。所有的坐標(biāo)都是相對于空間內(nèi)部的;
->4崭孤,onLayout是最后確定ViewGroup在parentView中占據(jù)的位置类嗤。如果是動態(tài)加載,可以在這一步對多有的childView確定布局辨宠;
->5遗锣,childView在onLayout之后,也布局位置確定嗤形,接下來精偿,就是ViewGroup對所有的childView分派draw的任務(wù),直接復(fù)寫dispatchDraw,所有的childView就會調(diào)用ondraw.