You are on page 1of 4

27/04/2011 User Define Control (Text View for ex…

网易 博客 摄影 发现好博客 群博客召集令 登录 注册

鹏既是我
不经历风雨,怎能见彩虹

首页 日志 相册 音乐 收藏 博友 关于我

日志

Design My ListView Relative Layout

User Define Control (Text View for example) Richie P

加博友 关注他
IT行业 2008-12-15 12:00:32 阅读1299 评论0 订阅 字号: 大 中 小

(1) Define attributes.XML for my control 最新日志

伤逝
---------------------------------------------------------
古文的逝去
<?xml version="1.0" encoding="utf-8"?> CPU与外设(转百度好问)
<resources> 【转载】NAND 和NOR …
<declare-styleable name="LabelView"> Relative Layout

<attr name="text" format="string" /> User Define Control (T…

<attr name="textColor" format="color" />


<attr name="textSize" format="dimension" /> 首页推荐

</declare-styleable> 穿孔狂人的嘴巴最给力
</resources> 马赛:适合裸晒的地方

-------------------------------------------------------------- 震撼!美丽的自然之心
唐人街吃顿饭才45美金
(2)Define my java file of control 巨蟒剖腹竟有上百条小…
美女真空上阵演绎黄色…
----------------------------------------------------------
更多>>
public class CustomControl extends View {
Paint p = new Paint();
Color clr = null;
String text = null;
int txtSize = 0;
private int mAscent;

public CustomControl(Context context, AttributeSet attrs) {


super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LabelView);

CharSequence s = a.getString(R.styleable.LabelView_text);
if (s != null) {
setText(s.toString());
}
int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
if (textSize > 0) {
setTextSize(textSize);
}
int clr = a.getColor(R.styleable.LabelView_textColor, Color.rgb(0, 0, 0));
this.setColor(clr);

…163.com/…/2089095620081115003… 1/4
27/04/2011 User Define Control (Text View for ex…
a.recycle();
}

public void setTextSize(int size) {


p.setTextSize(size);
requestLayout();
invalidate();
}

public void setColor(int clr){


//p.setColor(clr.rgb(clr.RED, clr.GREEN, clr.BLUE));
p.setColor(clr);
requestLayout();
invalidate();
}

public void setText(String Text) {


text = Text;
requestLayout();
invalidate();
}

public String getText(){


return text;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
result = (int) p.measureText(text) + getPaddingLeft()
+ getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
return result;
}

private int measureHeight(int measureSpec) {


int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

mAscent = (int) p.ascent();


if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = (int) (-mAscent + p.descent()) + getPaddingTop()
+ getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);

…163.com/…/2089095620081115003… 2/4
27/04/2011 User Define Control (Text View for ex…
}
}
return result;
}

public CustomControl(Context context) {


super(context);
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawText(text, getPaddingLeft(), getPaddingTop() - mAscent, p);
}
}

------------------------------------------------------------------------

(3)use the control

-------------------------------------

xmlns:app="http://schemas.android.com/apk/res/com.china.myapilist"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.china.myapilist.customcontrol.CustomControl

android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:textColor="#ffff0000"
app:text="Red"/>

<com.china.myapilist.customcontrol.CustomControl

android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:textColor="#ff0000ff"
app:text="Blue" app:textSize="20dp"/>

<com.china.myapilist.customcontrol.CustomControl

android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:text="Green" app:textColor="#ff00ff00" />
<ExpandableListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id = "@+id/mylist"
/>
</LinearLayout>
-------------------------------------------------------------------------

Effect:

…163.com/…/2089095620081115003… 3/4
27/04/2011 User Define Control (Text View for ex…

分享到: 阅读(1299) | 评论(0) | 引用 (0) | 举报

Design My ListView Relative Layout

最近读者

登录后,您可以
在此留下足迹。
yjg200… igohu… 锦绣シ鈊 不再犹豫 Kevin.… 斌の少

评论

点击登录 | 昵称:

发表

公司简介 - 联系方法 - 招聘信息 - 客户服务 - 隐私政策 - 博客风格 - 手机博客 - 订阅此博客

网易公司版权所有 ©1997-2011

…163.com/…/2089095620081115003… 4/4

You might also like