2、继承和组合,绘制方式定制控件
定制控件的方式
1、继承其他控件类。 例如,EditText,是继承TextView.
例子:带图标的文本框

public IconTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IconTextView); resourceId = typedArray.getResourceId(R.styleable.IconTextView_iconSrc, 0); if (resourceId > 0) bitmap = BitmapFactory.decodeResource(getResources(), resourceId); iconPosition = typedArray.getInt(R.styleable.IconTextView_iconPosition, 0); } @Override protected void onDraw(Canvas canvas) { if (bitmap != null) { // 从原图上截取图像的区域,在本例中为整个图像 Rect src = new Rect(); // 将截取的图像复制到bitmap上的目标区域,在本例中与复制区域相同 Rect target = new Rect(); src.left = 0; src.top = 0; src.right = bitmap.getWidth(); src.bottom = bitmap.getHeight(); int textHeight = (int) getTextSize(); int left = 0; if (iconPosition == 1) { left = (int) getPaint().measureText(getText().toString()) + 2; } target.left = left; // 计算图像复制到目录区域的纵坐标。由于TextView中文本内容并不是从最顶端开始绘制的,因此,需要重新计算绘制图像的纵坐标 target.top = (int) ((getMeasuredHeight() - getTextSize()) / 2) + 1; target.bottom = target.top + textHeight; // 为了保证图像不变形,需要根据图像高度重新计算图像的宽度 target.right = left + (int) (textHeight * (bitmap.getWidth() / (float) bitmap .getHeight())); // 开始绘制图像 canvas.drawBitmap(bitmap, src, target, getPaint()); // 将TextView中的文本向右移动一定的距离(在本例中移动了图像宽度加2个象素点的位置) if (iconPosition == 0) canvas.translate(target.right + 2, 0); } super.onDraw(canvas); } }
[html] view plaincopy 01.<?xml version="1.0" encoding="utf-8"?> 02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 03. xmlns:mobile="http://cn.eoe.icon.textview" android:orientation="vertical" 04. android:layout_width="fill_parent" android:layout_height="fill_parent"> 05. <cn.eoe.widget.IconTextView 06. android:layout_width="fill_parent" android:layout_height="wrap_content" 07. android:text="第一个图标" mobile:iconSrc="@drawable/android" /> 08. <cn.eoe.widget.IconTextView 09. android:layout_width="fill_parent" android:layout_height="wrap_content" 10. android:text="第二个图标" android:textSize="24dp" mobile:iconSrc="@drawable/android" /> 11. <cn.eoe.widget.IconTextView 12. android:layout_width="fill_parent" android:layout_height="wrap_content" 13. android:text="第三个图标" android:textSize="36dp" mobile:iconSrc="@drawable/android" /> 14. <cn.eoe.widget.IconTextView 15. android:layout_width="fill_parent" android:layout_height="wrap_content" 16. android:text="第四个图标" android:textSize="48dp" mobile:iconSrc="@drawable/android" /> 17. <cn.eoe.widget.IconTextView 18. android:layout_width="fill_parent" android:layout_height="wrap_content" 19. android:text="第五个图标" android:textSize="36dp" mobile:iconSrc="@drawable/android" /> 20. <cn.eoe.widget.IconTextView 21. android:layout_width="fill_parent" android:layout_height="wrap_content" 22. android:text="第六个图标" android:textSize="24dp" mobile:iconSrc="@drawable/android" /> 23. <cn.eoe.widget.IconTextView 24. android:layout_width="fill_parent" android:layout_height="wrap_content" 25. android:text="第七个图标" mobile:iconSrc="@drawable/android" /> 26. 27.</LinearLayout>
2、组合方式:当前控件类从容器类(布局类,例如linearLayout等)继承,并将若干个控件到当前的容器中。
例子:带标签的文本编辑框:
- package cn.eoe.widget;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import cn.eoe.label.edittext.R;
- public class LabelEditText extends LinearLayout
- {
- private TextView textView;
- private String labelText;
- private int labelFontSize;
- private String labelPosition;
- public LabelEditText(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- // 读取labelText属性的资源ID
- int resourceId = attrs.getAttributeResourceValue(null, "labelText", 0);
- // 未获得资源ID,继续读取属性值
- if (resourceId == 0)
- labelText = attrs.getAttributeValue(null, "labelText");
- // 从资源文件中获得labelText属性的值
- else
- labelText = getResources().getString(resourceId);
- // 如果按两种方式都未获得labelTex属性的值,表示未设置该属性,抛出异常
- if (labelText == null)
- {
- throw new RuntimeException("必须设置labelText属性.");
- }
- // 获得labelFontSize属性的资源ID
- resourceId = attrs.getAttributeResourceValue(null, "labelFontSize", 0);
- // 继续读取labelFontSize属性的值,如果未设置该属性,将属性值设为14
- if (resourceId == 0)
- labelFontSize = attrs.getAttributeIntValue(null, "labelFontSize",
- 14);
- // 从资源文件中获得labelFontSize属性的值
- else
- labelFontSize = getResources().getInteger(resourceId);
- // 获得labelPosition属性的资源ID
- resourceId = attrs.getAttributeResourceValue(null, "labelPosition", 0);
- // 继续读取labelPosition属性的值
- if (resourceId == 0)
- labelPosition = attrs.getAttributeValue(null, "labelPosition");
- // 从资源文件中获得labelPosition属性的值
- else
- labelPosition = getResources().getString(resourceId);
- // 如果未设置labelPosition属性值,将该属性值设为left
- if (labelPosition == null)
- labelPosition = "left";
- String infService = Context.LAYOUT_INFLATER_SERVICE;
- LayoutInflater li;
- // 获得LAYOUT_INFLATER_SERVICE服务
- li = (LayoutInflater) context.getSystemService(infService);
- LinearLayout linearLayout = null;
- // 根据labelPosition属性的值装载不同的布局文件
- if("left".equals(labelPosition))
- linearLayout = (LinearLayout)li.inflate(R.layout.labeledittext_horizontal, this);
- else if("top".equals(labelPosition))
- linearLayout = (LinearLayout)li.inflate(R.layout.labeledittext_vertical, this);
- else
- throw new RuntimeException("labelPosition属性的值只能是left或top.");
- // 下面的代码从相应的布局文件中获得了TextView对象,并根据LabelTextView的属性值设置TextView的属性
- textView = (TextView) findViewById(R.id.textview);
- //textView.setTextSize((float)labelFontSize);
- textView.setTextSize(labelFontSize);
- textView.setText(labelText);
- }
- }
/labeledittext/res/layout/labeledittext_horizontal.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textview" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText android:id="@+id/edittext" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
/labeledittext/res/layout/labeledittext_vertical.xml:
<?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:id="@+id/textview" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText android:id="@+id/edittext" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
3、绘制控件:也就是控件类从View继承,并在onDraw方法中零绘制控件。例如,TextView.
posted on 2015-08-02 23:26 EverTriely 阅读(120) 评论(0) 收藏 举报
浙公网安备 33010602011771号