TextView加边框,自定义,上下左右四条线 颜色,想用哪个用哪个
1.这是一个自定义的TextView ,看吧,底下就是代码,应该都可以看懂,这里就不多说了
package com.example.admin.myutilsborder;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by admin on 2017/3/7.
*/
public class BorderTextView extends TextView {
private boolean top_border ;
private boolean bottom_border;
private boolean left_border ;
private boolean right_border ;
public BorderTextView(Context context) {
super(context);
}
public BorderTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BorderTextView);
top_border =ta.getBoolean(R.styleable.BorderTextView_top_border,false);
bottom_border= ta.getBoolean(R.styleable.BorderTextView_bottom_border,false);
left_border= ta.getBoolean(R.styleable.BorderTextView_left_border,false);
right_border= ta.getBoolean(R.styleable.BorderTextView_right_border,false);
}
private int sroke_width = 1;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
// 将边框设为黑色
//paint.setColor(android.graphics.Color.RED);
paint.setColor(Color.GREEN);
// paint.setColor(Color.BLUE);
//paint.setColor(android.graphics.Color.alpha(717171));
// 画TextView的4个边
//drawLine方法参数顺序 左 上左
if(left_border){
canvas.drawLine(0, 0, 0, this.getHeight() - sroke_width, paint);
}
if(top_border){
canvas.drawLine(0, 0, this.getWidth() - sroke_width, 0, paint);
}
if(right_border){
canvas.drawLine(this.getWidth() - sroke_width, 0, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
}
if(bottom_border) {
canvas.drawLine(0, this.getHeight() - sroke_width, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
}
//上
//canvas.drawLine(0, 0, this.getWidth() - sroke_width, 0, paint);
//左
//canvas.drawLine(0, 0, 0, this.getHeight() - sroke_width, paint);
//右
// canvas.drawLine(this.getWidth() - sroke_width, 0, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
//下
//canvas.drawLine(0, this.getHeight() - sroke_width, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
//super.onDraw(canvas);
}
}
2.报错了,是不是,别急
在valus目录下新建一个attrs文件夹,注意:我这里使用Studio开发
<resources> <declare-styleable name="BorderTextView">
<attr name="left_border" format = "string" />
<attr name="right_border" format = "string" />
<attr name="top_border" format = "string" />
<attr name="bottom_border" format = "string" />
<attr name="mTextColor" format="color" />
<attr name="mTextSize" format="dimension" />
</declare-styleable> </resources>
3.剩下的就是使用了,在XMl文件中
<com.example.admin.myutilsborder.BorderTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:top_border="true"
app:bottom_border="true"
app:left_border="true"
app:right_border="true"
android:text="Hello World!" />
<!--上面这里就是自定义的边框,需要上下左右哪个方向的线条就把它设置成true,不需要,直接删掉就好-->
4.最后一步,在Xml文件中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
注意,多加了下面这条语句
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent" 完了,就这样简单的搞定了,简单的自定义,我这里就不过多的解释了,在碰见几个TextView放在一起,如果都设置边框的话,会让一部分线条变粗,当然,背景图也完全可以解决,但是搞技术嘛,简单的写写
TextView加边框,自定义,上下左右四条线 颜色,想用哪个用哪个的更多相关文章
- android TextView加边框
为TextView加边框.须要在drawable建xml文件,里面设置shape来设置文本框的特殊效果. <?xml version="1.0" encoding=" ...
- Android 自定义水平进度条的圆角进度
有时项目中需要实现水平圆角进度,如下两种,其实很简单 下面开始看代码,先从主界面布局开始看起: <?xml version="1.0" encoding=" ...
- 安卓中经常使用控件遇到问题解决方法(持续更新和发现篇幅)(在textview上加一条线、待续)
TextView设置最多显示30个字符.超过部分显示...(省略号),有人说分别设置TextView的android:signature="true",而且设置android:el ...
- android开发(34) 自定义 listView的分割线( 使用xml drawable画多条线)
我遇到这样一个场景,我需要自定义 listView的分割线,而这个分割线是由两条线组成的,在使用xml drawable时遇到了困难. 注释:画两条线是为了实现 凹陷的效果,在绘图中一条暗线紧跟着一条 ...
- 自定义View画一条线
#import "PublishContextView.h" @implementation PublishContextView -(void)drawRect:(CGRect) ...
- Html-浅谈如何正确给table加边框
一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...
- 浅谈如何正确给table加边框
一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...
- 2017-11-28 Html-浅谈如何正确给table加边框
一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...
- Html-如何正确给table加边框
一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...
随机推荐
- Hibernate最简单教程
其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西, ...
- 【js 编程艺术】小制作三
1.html文件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- 利用享元模式来解决DOM元素过多导致的网页解析慢、卡死的问题
我也不知道应该为本文的思路取一个什么比较恰当的标题,但是感觉符合享元模式的思路. 在一些网页应用中,有时会碰到一个超级巨大的列表,成千上万行,这时大部份浏览器解析起来就非常痛苦了(有可能直接卡死). ...
- 双显卡笔记本安装CUDA+theano、tensorflow环境
原文出处:http://www.cnblogs.com/jacklu/p/6377820.html 个人知乎主页欢迎关注:https://www.zhihu.com/people/jack_lu,相信 ...
- C/C++中的联合体
C/C++中的联合体 利用union可以用相同的存储空间存储不同型别的数据类型,从而节省内存空间.当访问其内成员时可用"."和"->"来直接访问. 当多个 ...
- Javascript—①你好,世界!
新手Perfect教程之Javascript教程①-你好,世界! 前言:不知道Javascript是什么东东的自行度娘或google一下 Javascript在html<head>和< ...
- MS office2010 ppt自动放映的设置方法
在菜单“幻灯片放映”-“排练时间”,进入演示,然后设置每张幻灯片需要等待的时间后,手动切换到下一张(如图),一直放映结束,会提示你是否保存排练时间.然后在菜单“幻灯片放映”-“设置放映方式”,里面选择 ...
- CSS 中的相对量
CSS 属性的浏览器兼容性,请查询 Can I Use. 已经被标准废弃(Deprecated)的属性,没有列出. 欢迎您与我一同完善这个清单,提供数据的读者姓名将在文中标注. (通过评论方式提供遗漏 ...
- dev简单实现柱状图,曲线图
1.数据源代码: DataTable dt = new DataTable(); dt.Columns.Add("A"); dt.Columns.Add("B" ...
- 为 .NET Core 设计一个 3D 图形渲染库
原文地址:https://mellinoe.wordpress.com/2017/02/08/designing-a-3d-rendering-library-for-net-core/ 作者:ERI ...