android 为TextView添加边框
来源:互联网 发布:c语言二叉树的遍历 编辑:程序博客网 时间:2024/05/04 21:50
今天需要在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 = 1;@Overrideprotected void onDraw(Canvas canvas) {Paint paint = new Paint(); // 将边框设为黑色 paint.setColor(android.graphics.Color.BLACK); // 画TextView的4个边 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);}}
效果图如下:
使用的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添加边框
- Android--为TextView添加边框
- android 为TextView添加边框
- android 为TextView添加边框
- Android 为TextView 添加边框
- android 为TextView添加边框
- Android--为TextView添加边框
- android 为TextView添加边框
- android 为TextView添加边框
- android 为TextView添加边框
- Android中为textView添加边框
- android里TextView加下划线的几种方式 以及 为TextView添加边框
- android textView添加不同颜色的边框
- android开发 -为TextView加上边框
- Android开发之Shape的使用(为TextView等空间添加边框)
- 为android textview添加scrollbar
- 给TextView添加边框
- TextView 添加边框
- wofstream写中文失败,导致stream对象badbit
- 走进编程技术博客开启啦
- 21天教你学会C++
- ListView控件复用带来的问题及解决方法
- Android java.lang.NoClassDefFoundError的一种解决方法
- android 为TextView添加边框
- Mysql 体系结构系列之二
- [ctest]打印二进制数
- 如何用gcc编译生成动态链接库*.so文件 动态库
- jquery 基础知识
- scrollview同时嵌套listview和gridview 显示高度问题【只显示一行】
- 又遇乱码,GBK网页AJAX,PHP接收的中文字符是UTF-8编码的
- java中静态代码块的用法 static用法详解
- Spring2.5、Struts2、Ibatis开发框架搭建之(一)