安卓學習-介面-View的自訂

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   java   os   io   

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.