TextView加边框,自定义,上下左右四条线 颜色,想用哪个用哪个

摘要:
importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;publicBorderTextView(Contextcontext){super(context);

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));

// TextView4个边
//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加边框,自定义,上下左右四条线 颜色,想用哪个用哪个》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Fiddler抓包5-接口测试(Composer)【转载】机器学习环境配置系列六之jupyter notebook远程访问下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

solr的配置文件及其含义

solr与.net系列课程(二)solr的配置文件及其含义        solr与.net系列课程(二)solr的配置文件及其含义        本节内容还是不会涉及到.net与数据库的内容,但是不要着急,这都是学时solr必学要掌握的东西,solr可不是像其他的dll文件一样,只需要引用就能调出方法与数据的,你不配置好是无法使用,前两节主要是起铺垫...

C# winform 动态添加控件 以及 事件

C# winform 动态添加控件 以及 事件以下代码将在加载窗体时动态生成若干按钮,并排列显示在winform上using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using Sys...

MAC连接安卓手机通过adb指令安装apk

Android的apk可以通过adb命令来安装。在MAC电脑上,如果想通过命令行的方式给安卓手机安装apk,需要做以下操作; 一句话概括就是:将安卓SDK的adb命令添加到环境变量中,然后通过adb install方式将apk从电脑上安装到手机中。 具体操作如下: 1、点击Finder,右键选择“前往文件夹”,输入“/etc”,在“操作”中选择“显示简介”...

tomcat9配置https

首先使用以下命令生成密钥库 keytool -genkey -alias tomcat -keyalg RSA 我输入的密钥口令是123456 组织单位名称就是你的域名,我的就是localhost 得到一个.keystore的文件,我把这个文件复制到tomcat的conf文件目录里面 在tomcat的server.xml中添加一个connector,代码如...

下拉菜单(css)

<!DOCTYPE html><html> <head>  <meta charset="utf-8" />  <title></title>  <style type="text/css">   *{margin:0;padding:0;}   #nav{backgrou...

Xamarin.Forms学习系列之Syncfusion 制作图形报表

Syncfusion是一家微软生态下的第三方组件/控件供应商,除了用于HTML5和JavaScript的控件外,他们产品还涉及如下领域: WEB ASP.NET MVC ASP.NET WebForms HTML5/JavaScript LightSwitch Silverlight MOBILE iOS Android Windows Phone...