关于如何自定义一个控件,首先配置attr文件,在res-->value目录下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="text" format="string"/>
</declare-styleable>
</resources>
<!-- name="CustomView"控件名称 得到TypedArray时用 -->
<!-- name="textColor" 对应test:textColor -->
<!-- format="color" 对应构造方法里a.getColor(R.styleable.CustomView_textColor, 0xFFFFFFFF); -->
这是Java文件,继承自View类,paint一个控件,设置字,或者其他
package com.example.myview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
public class CustomView extends View {
private Paint mPaint2;
private String mText = "drawText";
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint2 = new Paint();
// TypedArray是存放资源的array,1.通过上下文得到这个数组,attrs是构造函数传进来的,对应attrs.xml
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
// 获得xml里定义的属性,格式为 名称_属性名 后面是默认值
int textColor = a.getColor(R.styleable.CustomView_textColor, 0xFFFFFFFF);
float textSize = a.getDimension(R.styleable.CustomView_textSize, 35);
mText = a.getString(R.styleable.CustomView_text);
mPaint2.setColor(textColor);//这两个是默认的
mPaint2.setTextSize(textSize);
// 为了保持以后使用该属性一致性,返回一个绑定资源结束的信号给资源
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint2.setStyle(Style.FILL);
canvas.drawText(mText, 10, 60, mPaint2);
}
}
这是xml布局文件,引用自己的命名控件,引用自己定义的控件
<?xml version="1.0" encoding="utf-8"?>
<!-- xmlns:test="http://schemas.android.com/apk/res/ethan.customview1" 包名 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.example.myview"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.myview.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
test:textColor="#f00"
test:text="手动vgsh"
test:textSize="40sp" />
</LinearLayout>
最后在Activity中显示这个布局
package com.example.myview;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mytext);
}
}
自定义成功,效果图改天再上。
另外:今天发现一种:通过style来设置,这样不是完全的自定义文件,就类似于网页的CSS文件,设置style,一样一样的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"
style="@style/TextView" />
<EditText android:id="@+id/EditText01" android:layout_height="wrap_content"
style="@style/EditText" android:layout_width="fill_parent"
android:text="类似Button的EditText"></EditText>
<EditText android:id="@+id/EditText02" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="普通的EditText"></EditText>
<Button android:id="@+id/Button01" android:layout_height="wrap_content"
style="@style/Button" android:layout_width="fill_parent" android:text="类似EditText的Button"></Button>
</LinearLayout>
总结:控件的字,背景容易定义,在控件的宽和高内容包括,父对齐,这些还在专研之中,作为菜鸟android程序员,希望越来越进步!