首页 > 代码库 > 安卓学习-界面-View的自定义

安卓学习-界面-View的自定义

android的所有UI控件,都是基于View的,因此特意重点学习了下这个,为后面学习其他控件打下基础。

http://www.360doc.com/content/14/0102/12/12050012_342019150.shtml

重新时常用覆盖的方法

package com.example.ddddddd;import android.content.Context;import android.graphics.Canvas;import android.graphics.Rect;import android.util.AttributeSet;import android.util.Log;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.View;import android.widget.Button;public class MyButton extends Button{    public MyButton(Context context, AttributeSet attrs) {        super(context, attrs);    }    //窗口加载这个组件时会执行    protected void onFinishInflate() {        super.onFinishInflate();        Log.d("test", "我被XML加载了");    }        //看不懂,不知道什么意思,解释如下    //用来检测View组件和他包含的所有子组件的大小    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }        //当该组件需要分配其子组件位置、大小时会调用该方法    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {        super.onLayout(changed, left, top, right, bottom);    }        //该组件大小被改变时会调用该方法    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        Log.d("test", "被改变大小了,原来w="+oldw+"y="+oldh+" 现在w="+w+"y="+h);        super.onSizeChanged(w, h, oldw, oldh);    }        //要绘制该组件内容时回调该方法    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);    }        //光标在这个组件上,并且按键被按下时调用该方法    public boolean onKeyDown(int keyCode, KeyEvent event) {        Log.d("test", "按键"+keyCode+"按下了");        return super.onKeyDown(keyCode, event);    }        //光标在这个组件上,并且按键松开时调用该方法    public boolean onKeyUp(int keyCode, KeyEvent event) {        Log.d("test", "按键"+keyCode+"松开了");        return super.onKeyUp(keyCode, event);    }    //发生轨迹球事件,不知道是个什么东西    public boolean onTrackballEvent(MotionEvent event) {        return super.onTrackballEvent(event);    }        //发生触摸屏事件,触发此方法    public boolean onTouchEvent(MotionEvent event) {        Log.d("test", "我被触摸了,位置 X:"+event.getX()+" Y:"+event.getY());        return super.onTouchEvent(event);    }        //当得到或失去焦点,触发    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {        if(gainFocus){            Log.d("test", "得到焦点");        }else{            Log.d("test", "失去焦点");        }        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);    }        //该组件放入某个窗口时,触发    protected void onAttachedToWindow() {        super.onAttachedToWindow();    }        //组件从窗口上分离,触发    protected void onDetachedFromWindow() {        Log.d("test", "分离");        super.onDetachedFromWindow();    }        //可见性发生改变时触发,一般调用setVisibility(View.GONE)    protected void onVisibilityChanged(View changedView, int visibility) {        Log.d("test", "摧毁");        super.onVisibilityChanged(changedView, visibility);    }}
View Code

一个跟随手指移动的球

 

 

public class MyView extends View{    public MyView(Context context) {        super(context);    }        public MyView(Context context, AttributeSet attrs) {        super(context, attrs);    }        //定义一个画笔    private Paint paint=new Paint();    private float cx=0;    private float cy=0;        protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //设置画笔颜色,这里制定红色        paint.setColor(Color.RED);        //绘制一个位置在cx,cy的半径15,的圆        canvas.drawCircle(cx, cy, 15, paint);    }    public boolean onTouchEvent(MotionEvent event) {        cx=event.getX();        cy=event.getY();        //重绘        invalidate();        return true;    }}
View Code

 标题,参照网上的一个帖子做的

title.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="50dp"    android:background="#ffcb05" >    <Button        android:id="@+id/button_left"        android:layout_width="60dp"        android:layout_height="40dp"        android:layout_centerVertical="true"        android:layout_marginLeft="5dp"        android:background="@drawable/back_button"        android:textColor="#fff" />    <TextView        android:id="@+id/title_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="标题"        android:textColor="#fff"        android:textSize="20sp" /></RelativeLayout>
View Code

组件TitleView.java

public class TitleView extends FrameLayout {    //左边返回的按钮    private Button leftButton;    //中间的标题文字    private TextView titleText;    //初始化    public TitleView(Context context, AttributeSet attrs) {        super(context, attrs);        //获取XML配置文件        LayoutInflater.from(context).inflate(R.layout.title, this);        //获取标题文字组件        titleText = (TextView) findViewById(R.id.title_text);        //获取返回按钮组件        leftButton = (Button) findViewById(R.id.button_left);        //点击事件        leftButton.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                ((Activity) getContext()).finish();            }        });    }    public void setTitleText(String text) {        titleText.setText(text);    }    public void setLeftButtonText(String text) {        leftButton.setText(text);    }    public void setLeftButtonListener(OnClickListener l) {        leftButton.setOnClickListener(l);    }}
View Code

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.example.ddddddd.TitleView        android:id="@+id/title_view"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </com.example.ddddddd.TitleView></RelativeLayout>
View Code