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. selenium相关面试题

    selenium中如何判断元素是否存在? selenium中hidden或者是display = none的元素是否可以定位到? selenium中如何保证操作元素的成功率?也就是说如何保证我点击的元 ...

  2. 同步、异步、阻塞、非阻塞IO

    在网上看到一篇对这四个概念比较清晰的分析的文章:http://blog.csdn.net/historyasamirror/article/details/5778378.结合自己一直在学习Java ...

  3. MyBatis 源码分析——动态代理

    MyBatis框架是如何去执行SQL语句?相信不只是你们,笔者也想要知道是如何进行的.相信有上一章的引导大家都知道SqlSession接口的作用.当然默认情况下还是使用DefaultSqlSessio ...

  4. xshell无法连接到linux主机原因分析

    xshell连接linux主机时,会出现错误:Could not connect to '192.168.89.144' (port 22): Connection failed.  但是这时能pin ...

  5. The type or namespace name '****' could not be found

    偶尔会在编译时出现“The type or namespace name '****' could not be found (are you missing a using directive or ...

  6. JAVA中的访问修饰符和包

    一.JAVA访问修饰符 访问修饰符,用来控制类中成员的可见性 有四个访问修饰符,分别是:default,private,public,protected 1.default(默认):默认权限,不用写的 ...

  7. setObject:forKey和setValue:forKey的区别

    setObject:forKey: 是NSMutableDictionary类的方法                              key参数类型可以是任意类型对象             ...

  8. R语言爬虫 rvest包 html_text()-html_nodes() 原理说明

    library(rvest) 例子网页:http://search.51job.com/jobsearch/search_result.php?fromJs=1&jobarea=010000% ...

  9. Codeforces Round #396.D

    D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...

  10. 读书笔记 effective c++ Item 16 成对使用new和delete时要用相同的形式

    1. 一个错误释放内存的例子 下面的场景会有什么错? std::]; ... delete stringArray 一切看上去都是有序的.new匹配了一个delete.但有一些地方确实是错了.程序的行 ...