android的控件框架


控件構成了應用界面的基本單元,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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章