控件构成了应用界面的基本单元,android的控件都派生自android.view.View类,另外还有一个极其重要的ViewGroup类(可以容纳其他组件的控件),
一般的界面组件控件树关系图如下
于此同时每棵控件树,都会有一个ViewParent对象与其根控件绑定,此ViewParent对象就是整个控件树中交互事件的控制中心,每个控件对象都会包含指向ViewParentd对象的指针,当控件属性发生变化的时会通知该ViewParent,由ViewParent对象统一自顶向下分发该事件。如:当用户按键,触摸灯操作时,产生交互时间,就会沿着控件树自顶向下传播(如果需要则截获,否则向下传播)。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK)
return true;//接收该事件处理
return false;//继续向下传播
}
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,FrameLayout,LinearLayout,RelativeLayout,SlidingDrawer,AdapterView<T extends Adapter>,FragmentBreadCrumbs
ViewGroup派生出的间接子类有:AbsListView,AbsSpinner, AdapterViewAnimator, AdapterViewFlipper, AppWidgetHostView, CalendarView, DatePicker, DialerFilter, ExpandableListView, Gallery, GestureOverlayView,GridView,HorizontalScrollView, ImageSwitcher,ListView,
自定义控件:来看一个自定义控件的例子,控件的XML布局(custombutton.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="4dp"
android:clickable="true"
android:focusable="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/darkGray" />
</LinearLayout>
CustomButton
CustomButton类:
public class CustomButton extends LinearLayout {
ImageView imageView;
TextView textView;
CustomButtonClickListener clickListener;
public CustomButton(Context context) {
super(context, null);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custombutton, this);
imageView = (ImageView)findViewById(R.id.imageView);
textView = (TextView)findViewById(R.id.textView);
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(clickListener != null) {
clickListener.imageClick();
}
}
});
}
public void setImageViewResource(int resId) {
imageView.setImageResource(resId);
}
public void setTextViewResource(int resId) {
String str = PhyFitApplication.getInstance().getResources().getString(resId);
textView.setText(str);
}
public void setTextViewTextColor(int color) {
textView.setTextColor(color);
}
}
调用时候xml
<com.physicalfitness.ui.module.CustomButton
android:id="@+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/btn_green" >
</com.physicalfitness.ui.module.CustomButton>