今天需要在TextView上面添加一个边框,但是TextView本身不支持边框,所以只能采用其他方式,在网上查询了一下,主要有三种方式可以实现1.带有边框的透明图片2.使用xml的shape设置3继承TextView覆写onDraw方法。

方法一:

带有透明图片的背景图,这个没有什么好将的,自己制作一个就行 ,然后设置background就可以了

方法二:

通过shape来设置背景图片

首先一个textview_border.xml文件放在drawable文件夹里面

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

为要添加边框的TextView添加一个background

android:background="@drawable/textview_border"

方法三:

编写一个继承TextView类的自定义组件,并在onDraw事件方法中画边框。

    package com.example.test;  

    import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView; @SuppressLint("DrawAllocation")
public class BorderTextView extends TextView{ public BorderTextView(Context context) {
super(context);
}
public BorderTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private int sroke_width = ;
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
// 将边框设为黑色
paint.setColor(android.graphics.Color.BLACK);
// 画TextView的4个边
canvas.drawLine(, , this.getWidth() - sroke_width, , paint);
canvas.drawLine(, , , this.getHeight() - sroke_width, paint);
canvas.drawLine(this.getWidth() - sroke_width, , this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
canvas.drawLine(, this.getHeight() - sroke_width, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
super.onDraw(canvas);
}
}

效果图如下:

使用的Xml布局内容如下:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:layout_width="120dp"
android:layout_height="80dp"
android:background="@drawable/textview_border"
android:text="方法二"
android:textColor="#FF000000"
android:id="@+id/test"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/> <com.example.test.BorderTextView
android:layout_width="120dp"
android:layout_height="80dp"
android:text="方法三"
android:id="@+id/test3"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"
></com.example.test.BorderTextView>
</RelativeLayout>

android 为TextView添加边框的更多相关文章

  1. Android 为 TextView 添加超链接 (网址,邮件,电话)

    <string name="info">Cette application a été développée par <a href="http://w ...

  2. Android 给TextView添加点击事件

    首先设定TextView的clickable属性为true. 可以在布局文件中进行设定,比如: <TextView android:id="@+id/phone" andro ...

  3. Android开发:TextView添加超链接的简便方法

    TextView中加入超链接的方式很多,但下面的方式应该的最简便合理的 strings.xml中定义字串 <string name="blog"><a href= ...

  4. Android中TextView添加删除线

    项目中的需求~~~~ 商城中物品的一个本身价格,还有一个就是优惠价格...需要用到一个删除线. public class TestActivity extends Activity { private ...

  5. Android供TextView添加多个点击文字

    我们使用社会性软件的过程中会或多或少像别人的帖子点,图. : 能够看到用户页面显示出来的仅仅是点了赞的用户的名称,点击这些名称能够进入到该用户的主页.我们就来实现相似的效果.直接上代码吧. @Over ...

  6. Android 学习心得 TextView 添加垂直滚动条

    XMl文件中: 添加android:scrollbars="vertical" 添加android:maxLines="10"  //设置一页最多显示10行 a ...

  7. Android 编程下 TextView 添加链接的一种方式

    通过如下这种方式给 TextView 添加的链接支持链接样式.点击事件.href 样式,代码如下: package cn.sunzn.tlink; import android.app.Activit ...

  8. 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  9. android textview添加滚动条

    给textview添加滚动条 方式一: xml代码: //设置滚动条的方向 android:scrollbars="vertical" java中设置 tView=(TextVie ...

随机推荐

  1. Holding Bin-Laden Captive!(hdoj1085)代码并未完全看懂

    We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But rec ...

  2. JSON.parse()和JSON.stringify()的用法

    JSON.parse()是用于从一个字符串中解析出json对象,如下所示 var str = '{"name":"flsummer","age&quo ...

  3. FPGA的LE数与门数的关系(转)

    一般而言FPGA等效门数的计算方法有两种 一是把FPGA基本单元(如LUT+FF,ESB/BRAM)和实现相同功能的标准门阵列比较,门阵列中包含的门数即为该FPGA基本单元的等效门数,然后乘以基本单元 ...

  4. js设计模式

    http://www.csdn.net/article/2011-09-02/303983 阐明JavaScript设计模式.CSDN研发频道对此文进行了整理选取部分内容,供开发者学习.参考. 内容如 ...

  5. 可获取公网IP的网址

    由于代理检验需要,现在小站经受不住大流量测试,于是多收集了一些. http://1111.ip138.com/ic.asp, http://ip.360.cn/IPShare/info, http:/ ...

  6. WPF中动态更新TextBlock文字中的超链接,文本

    1.------------------------------------------------------------------------- 修改超链接的文本文字: <TextBloc ...

  7. Bayesian Formulation on Cooperative Tracking

    Suppose a joint state representing a set of \(N_{n}\) nodes moving in a field\[    \textbf{X}=    \b ...

  8. 【具体数学 读书笔记】1.2 Lines in the Plane

    本节介绍平面划分问题,即n条直线最多把一个平面划分为几个区域(region). 问题描述: "What is the maximum number Ln of regions defined ...

  9. linux shell自定义函数(定义、返回值、变量作用域)介绍

    http://www.jb51.net/article/33899.htm linux shell自定义函数(定义.返回值.变量作用域)介绍 linux shell 可以用户定义函数,然后在shell ...

  10. Swift String 一些常用方法

    //字符串 //1 判断字符串是否为空 var test1Str="" var test1Str2:String = String(); println("test1St ...