<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 自定义属性开始 --> <declare-styleable name="BorderTextView"> <attr name="layout_borders" format="boolean"></attr> <attr name="layout_borderLeft" format="boolean"></attr> <attr name="layout_borderTop" format="boolean"></attr> <attr name="layout_borderRight" format="boolean"></attr> <attr name="layout_borderBottom" format="boolean"></attr> </declare-styleable> <!-- 自定义属性结束 --> </resources>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:greendroid="http://schemas.android.com/apk/res/com.llew.e" android:orientation="vertical" android:layout_width="fill_parent" android:background="#ffffff" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:text="@string/hello" android:textColor="#000000" /> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="左侧带有边框" android:layout_margin="10dip" greendroid:layout_borderLeft="true" android:textSize="20sp" android:textColor="#aabbcc"> </com.llew.e.view.wedgit.BorderTextView> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顶部带有边框" android:layout_margin="10dip" greendroid:layout_borderTop="true" android:textSize="20sp" android:textColor="#bbccaa"> </com.llew.e.view.wedgit.BorderTextView> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="右侧带有边框" android:layout_margin="10dip" greendroid:layout_borderRight="true" android:textSize="20sp" android:textColor="#ccaabb"> </com.llew.e.view.wedgit.BorderTextView> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="底部带有边框" android:layout_margin="10dip" greendroid:layout_borderBottom="true" android:textSize="20sp" android:textColor="#abcabc"> </com.llew.e.view.wedgit.BorderTextView> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="四周带有边框" android:layout_margin="10dip" greendroid:layout_borders="true" android:textSize="20sp" android:textColor="#cbacba"> </com.llew.e.view.wedgit.BorderTextView> </LinearLayout>
public class BorderTextView extends TextView {
/**
* 四周是否带有边框【true:四周带有边框】【false:四周不带边框】
*/
boolean borders = false;
/**
* 左边是否带有边框【true:左侧带有边框】【false:左侧不带边框】
*/
boolean borderLeft = false;
/**
* 顶部是否带有边框【true:顶部带有边框】【false:底部不带边框】
*/
boolean borderTop = false;
/**
* 右侧是否带有边框【true:右侧带有边框】【false:右侧不带边框】
*/
boolean borderRight = false;
/**
* 底部是否带有边框【true:底部带有边框】【false:底部不带边框】
*/
boolean borderBottom = false;
/**
* 边框颜色
*/
String textColor = "#ff000000";
public BorderTextView(Context context) {
this(context, null);
}
public BorderTextView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
public BorderTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 获取自定义属性集
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.BorderTextView);
// 是否设置全部边框,默认为false
borders = typedArray.getBoolean(
R.styleable.BorderTextView_layout_borders, false);
// 是否设置左侧边框,默认为false
borderLeft = typedArray.getBoolean(
R.styleable.BorderTextView_layout_borderLeft, false);
// 是否设置顶部边框,默认为false
borderTop = typedArray.getBoolean(
R.styleable.BorderTextView_layout_borderTop, false);
// 是否设置右侧边框,默认为false
borderRight = typedArray.getBoolean(
R.styleable.BorderTextView_layout_borderRight, false);
// 是否设置底部边框,默认为false
borderBottom = typedArray.getBoolean(
R.styleable.BorderTextView_layout_borderBottom, false);
// 获取文本颜色值,用来画边框的,便于和文本颜色匹配
textColor = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/android", "textColor");
typedArray.recycle();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
// 创建画笔
Paint paint = new Paint();
// 获取该画笔颜色
int color = paint.getColor();
// 设置画笔颜色
paint.setColor(Color.parseColor(textColor));
// 如果borders为true,表示左上右下都有边框
if (borders) {
canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
this.getHeight() - 1, paint);
canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
this.getHeight() - 1, paint);
} else {
if (borderLeft) {
// 画左边框线
canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
}
if (borderTop) {
// 画顶部边框线
canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
}
if (borderRight) {
// 画右侧边框线
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
this.getHeight() - 1, paint);
}
if (borderBottom) {
// 画底部边框线
canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
this.getHeight() - 1, paint);
}
}
// 设置画笔颜色归位
paint.setColor(color);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有