2013年8月7日 星期三

View 淺說(一)

先New 一個專案 MyView,定義res/layout/main.xml,包含兩個LinearLayout容器。一個Button


<LinearLayout 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"
    android:orientation="vertical"
   >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <Button 
        android:id="@+id/save"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Save"
        /> 
</ LinearLayout >
</ LinearLayout >


在專案中的src 中 NEW 一個新的Class myview 繼承自View 這個類別。myview 必須實作建構子
myview。 利用Paint()函數,抓取一個預設的paint ,並用setBackgroundColor()為View 上色



public class myview extends View {
private Paint paint;
public myview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//Create a new paint with default settings.
paint=new Paint();
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(4); 
setBackgroundColor(Color.BLACK);
}
}



存檔後,修改l/res/layout/main.xml ,於LinearLayout中加入自訂的View,取名為vv。

 <com.example.myview.myview

        android:id="@+id/vv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </com.example.myview.myview>






沒有留言:

張貼留言