android 为TextView添加边框
来源:互联网 发布:php 流媒体 编辑:程序博客网 时间:2024/05/17 05:59
今天需要在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>
0 0
- 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 添加边框
- NoMoreFixedIps Zero fixed ips available
- nyoj47过河问题(贪心)
- 判断是否为系统应用的几种方法[整理]
- 把windows字体都写在下拉控件里和查找文件加下的文件名.txt
- DB2查询锁及解锁
- android 为TextView添加边框
- 图像处理中的L1-normalize 和L2-normalize
- [cocos2d-x菜鸟学习记录]解析示例helloWorld(上)
- Jquery AJAX POST和GET区别
- MTK: android: TD、HSPA、GPRS的projectconfig.mk配置:
- maven配置jetty出现Cannot load implementation hint 'org.mortbay.jetty.nio.SelectChannelConnector'错误
- FlexViewer Module 图表控件 Error #1009
- Android中文文档
- jsp和servlet的区别?