自定义的 View 的含义是通过扩展的方法,实现一个扩展 android.view.View 类的类,这个类的本质也是一个控件,通过它可以直接构建 UI。
参考示例程序:CustomView(ApiDemo=>Views=>CustomView )
源代码:com/example/android/apis/view/CustomView1.java
com/example/android/apis/view/LabelView.java
布局文件:custom_view_1.xml
CustomView 程序的运行结果如图所示:
布局文件 custom_view_1.xml 的如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.example.android.apis.view.LabelView
android:background="@drawable/red"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:text="Red"/>
<com.example.android.apis.view.LabelView
android:background="@drawable/blue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:text="Blue" app:textSize="20dp"/>
<com.example.android.apis.view.LabelView
android:background="@drawable/green"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:text="Green" app:textColor="#ffffffff" />
</LinearLayout>
这里使用的标签 com.example.android.apis.view.LabelView 不是 Android 框架层提供的 View 的一个子类,而是在自己的程序中实现的一个类。能在布局文件中使用的类,也都是 android.view.View 类的继承者。
这个 com.example.android.apis.view.LabelView,在源文件 LabView.java 中实现,其主要片段如下所示:
public class LabelView extends View {
public LabelView(Context context, AttributeSet attrs) {
super(context, attrs);
initLabelView();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.LabelView);
CharSequence s = a.getString(R.styleable.LabelView_text);
if (s != null) {
setText(s.toString());
}
setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));
int textSize =
a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
if (textSize > 0) {
setTextSize(textSize);
}
a.recycle();
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(mText, getPaddingLeft(),
getPaddingTop() - mAscent, mTextPaint);
}
}
在 LabelView 的构造函数中,通过 context.obtainStyledAttributes 获得 LabelView 所特有的几个属性。R.styleable.LabelView 这些内容在 res/values/的 attrs.xml 文件中进行了定义,内容如下所示:
<declare-styleable name="LabelView">
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
根据 LabView.java 实现的类名称,这样自定义的控件也可以在布局文件中使用,使用标签与类名相一致。R.styleable.LabelView_text
,R.styleable.LabelView_textColor
和 R.styleable.LabelView_textSize
是在源代码中使用的属性,它们与引用 LabelView 的布局文件中的 app:text,app:textColor 和 app:textSize 等几个属性相对应。作为公共的属性,LabelView 在实现上也应该具有公共的函数来设置这几个属性。这些函数如下所示:
public void setText(String text) {
mText = text;
requestLayout();
invalidate();
}
public void setTextSize(int size) {
mTextPaint.setTextSize(size);
requestLayout();
invalidate();
}
public void setTextColor(int color) {
mTextPaint.setColor(color);
invalidate();
}
以上的几个函数和几个 XML 中的属性对应的,如果没有他们,这些属性就只能在 XML 文件中指定,而不能在 JAVA 源文件中设置。
在 Android 的应用程序层,可以通过扩展 android.view.View 或者它的扩展者来实现自己的 View。