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 ...
随机推荐
- java算法 蓝桥杯 高精度加法
问题描述 在C/C++语言中,整型所能表示的范围一般为-231到231(大约21亿),即使long long型,一般也只能表示到-263到263.要想计算更加规模的数,就要用软件来扩展了,比如用数组或 ...
- svn git协同管理
项目开发过程中总有一些奇奇怪怪的需求出现. 我们的项目管理是使用SVN的,用SVN是历史原因,无法整个项目向Git切换.由于我需要管理SVN,做一些代码合并工作.每次合并都会遇到SVN代码需要对比查看 ...
- 测试员浅谈App测试的重点
近年来,手机app也时持续大热.基于安卓和ios的手机app,更是受到众多投资者的青睐.而手机软件测试行业也是如此. 现在听的最多的是web测试和App测试,但实际上两者本质上没有什么区别,性质都一样 ...
- iOS 插件化开发汇总 Small框架
应用插件化背景 目前很多应用功能越来越多,软件显得越来越臃肿.因此插件化就成了很多软件发展的必经之路,比如支付宝这种平台级别的软件: 页上密密麻麻的功能,而且还在增多,照这个趋势发展下去,软件包的大小 ...
- Java动态代理深入解析
要想了解Java动态代理,首先要了解什么叫做代理,熟悉设计模式的朋友一定知道在Gof总结的23种设计模式中,有一种叫做代理(Proxy)的对象结构型模式,动态代理中的代理,指的就是这种设计模式. 在我 ...
- Python 日志模块实例
python 打印对象的所有属性值: def prn_obj(obj): print '\n'.join(['%s:%s' % item for item in obj.__dict__.it ...
- block之--- 基本使用
block的类型:对象 官方文档描述如下 “Blocks are Objective-C objects, which means they can be added to collections l ...
- C语言 memcpy二维数组的复制
今天在实现二维数组的复制功能时,竟然出现了好多问题,还是太不小心了. 我们知道,平时进行矩阵复制,无非是二重循环进行赋值操作,所以今天想改用利用memcpy进行复制操作,当然一维数组的复制在上一篇文章 ...
- mdadm命令详解及实验过程
一.概念 mdadm是multiple devices admin的简称,它是Linux下的一款标准的软件 RAID 管理工具,作者是Neil Brown 二.特点 mdadm能够诊断.监控和收集详细 ...
- Unity编程标准导引-2.2Unity中的基本概念
2.2Unity中的基本概念 上述介绍提到了几个概念:游戏对象.场景.资源.相机,这个小节我们来深入了解,同时进行一些实践性操作.不过首先,我们需要大概了解一下Unity的工程文件夹. 2.2.1工程 ...