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加边框,自定义,上下左右四条线 颜色,想用哪个用哪个的更多相关文章

  1. android TextView加边框

    为TextView加边框.须要在drawable建xml文件,里面设置shape来设置文本框的特殊效果. <?xml version="1.0" encoding=" ...

  2. Android 自定义水平进度条的圆角进度

    有时项目中需要实现水平圆角进度,如下两种,其实很简单     下面开始看代码,先从主界面布局开始看起: <?xml version="1.0" encoding=" ...

  3. 安卓中经常使用控件遇到问题解决方法(持续更新和发现篇幅)(在textview上加一条线、待续)

    TextView设置最多显示30个字符.超过部分显示...(省略号),有人说分别设置TextView的android:signature="true",而且设置android:el ...

  4. android开发(34) 自定义 listView的分割线( 使用xml drawable画多条线)

    我遇到这样一个场景,我需要自定义 listView的分割线,而这个分割线是由两条线组成的,在使用xml drawable时遇到了困难. 注释:画两条线是为了实现 凹陷的效果,在绘图中一条暗线紧跟着一条 ...

  5. 自定义View画一条线

    #import "PublishContextView.h" @implementation PublishContextView -(void)drawRect:(CGRect) ...

  6. Html-浅谈如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...

  7. 浅谈如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...

  8. 2017-11-28 Html-浅谈如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...

  9. Html-如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...

随机推荐

  1. EntityFramework 简单入个门

    任何一个和数据相关的系统里,数据持久化都是一个不容忽视的问题. 一直以来,Java 平台出了很多 NB 的 ORM 框架,Hibernate.MyBatis等等..NET 平台上,ORM 框架这一块一 ...

  2. 让EFCore更疯狂些的扩展类库(二):查询缓存、分部sql、表名替换的策略配置

    前言 上一篇介绍了扩展类库的功能简介,通过json文件配置sql语句 和 sql语句的直接执行,这篇开始说明sql配置的策略模块:策略管理器与各种策略的配置. 类库源码:github:https:// ...

  3. jsp+servlet+javaBean+Dao

    一.Servlet程序各模块介绍1.JSP 用于显示.收集数据的部分.2.Servlet 用于验证数据.实例化JavaBean.调用DAO连接数据库.控制页面跳转3.DAO 用于连接数据库及进行数据库 ...

  4. sublime text 添加到鼠标右键功能

    安装sublime text的同学可能在安装的时候忘了设置sublime text的右键功能.那我们介绍如何添加. 我们要创建一个.reg为后缀的文件sublime_addright.reg.那么…… ...

  5. 如何在sublime中安装使用eslint

    1:首先你需要全局安装eslint npm install -g eslint 安装完成后在控制台 输入 eslint -v 有版本号说明就可以在npm中使用了,可以检查语法的错误处,但还不能在sub ...

  6. 初探Lambda表达式/Java多核编程【0】从外部迭代到内部迭代

    开篇 放假前从学校图书馆中借来一本书,Oracle官方的<精通Lambda表达式:Java多核编程>. 假期已过大半才想起来还没翻上几页,在此先推荐给大家. 此书内容及其简洁干练,如果你对 ...

  7. 源码(05) -- java.util.AbstractCollection<E>

    java.util.AbstractCollection<E> 源码分析(JDK1.7) ------------------------------------------------- ...

  8. 访问量分类统计(QQ,微信,微博,网页,网站APP,其他)

    刚准备敲键盘,突然想起今天已经星期五了,有点小兴奋,一周又这么愉快的结束,又可以休息了,等等..我好像是来写Java博客的,怎么变成了写日记,好吧,言归正传. 不知道大家有没有遇到过这样的需求:统计一 ...

  9. bppm与AD域集成

    1. 使用admin登录BMC ProactiveNet Operations Console,点击选项-> 集成编辑 2. 勾选LDAP集成方式,配置相关信息,点击应用 3. 查看配置文件%B ...

  10. JAVA--可变长参数

    可变长参数: 可变长参数可以接受任意个数的实参,形参实际上是一个数组. 语法形式: 方法名称(类型 参数1,类型 参数2,类型...可变长参数) *可变长参数一定是方法的最后一个参数 public v ...