http://my.eoe.cn/cocobaby/archive/801.html
经典好文:http://blog.csdn.net/carrey1989/article/details/11757409
对于像我一样的新手,我们习惯用android自带的控件和传统的布局方式。
但对于高手,布局却是如下:
<?xml version="1.0" encoding="utf-8"?>
其中A extends LinerLayout, B extends TextView.
上面的话引用自:http://blog.csdn.net/Android_Tutor/article/details/5499731
ok,不说废话。
首先,我们来认识一下android自带的控件构成。
比如button,我们知道,要添加一个button,我们首先应该在XML文件中声明一个button,然后在下面定义它的id,文本大小,颜色,背景等属性。之后我们要在java程序中引用这个button,就像下面:
public class MainActivity extends Activity {
private Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)this.findViewById(R.id.button1);
所以,对于我们的自定义控件来说,我们要做的有这么两点:
1.我们需要一个属性集,里面定义了一系列的属性,比如颜色,字体大小之类。
2.需要一个类继承View并覆写它的一些方法,尤其是构造函数。
对于第一条,我们要在res/values目录下新建一个文件,名字为attrs.xml,里面定义一些属性,如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
说明:name="MyView"的意思是我们这个属性集的名字为MyView。
attr name="textColor" format="color"的意思是有一个名为textColor的属性,格式为color。(android有很多格式,自行百度吧)。后一句同理。
对于第二条,看代码:
public class Sss extends View {
private Paint mPaint;
private Context mContext;
private int textColor;
private float textSize;
private static final String mString = "http://my.eoe.cn/754060";
public Sss(Context context) {
super(context);
mPaint = new Paint();
}
public Sss(Context context,AttributeSet attr)
{
super(context,attr);
mPaint = new Paint();
//从myview这个属性集(之前定义的名字)中取得属性列表
TypedArray a = context.obtainStyledAttributes(attr, R.styleable.MyView);
/*从属性集中获得颜色 ,getcolor的两个参数的含义是:当我们引用这个控件的XML文件中没有给出这个值的话,就使用第二个参数(默认值),就比如我们button控件,如果我们在XML文件中没有给出button的text属性值的话就默认为"text1"。R.stytle.textcolor在attrs.xml定义之后自动生成。*/
textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF);
textSize = a.getDimension(R.styleable.MyView_textSize, 36);
//Give back a previously retrieved StyledAttributes, for later re-use.
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
mPaint = new Paint();
//设置填充
mPaint.setStyle(Style.FILL);
mPaint.setColor(textColor);
mPaint.setTextSize(textSize);
//画一个矩形,前两个是矩形左上角坐标,后两个是右下角坐标
canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
//绘制文字
canvas.drawText(mString, 10, 120, mPaint);
}
到此我们的自定义控件算完成了,那怎么使用呢?
两步,
1.在XML中声明
2.java中引用这个控件
首先,看XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:asd="http://schemas.android.com/apk/res/com.example.test_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.test_1.Sss
android:id="@+id/sds"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
asd:textColor="#FFF"
asd:textSize="50px" />
</LinearLayout>
说明:xmlns:asd=" http://schemas.android.com/apk/res/com.example.test_1xmlns:asd为命名空间,asd随便娶个名字,方便后面调用(asd:textColor="#FFF "中 asd:textSize="50px")
com.example.test_1.Sss为包名+类名
然后在java程序中与普通的控件一样引用,只不过Button换成Sss,button1换成sds,你懂的~