zoukankan      html  css  js  c++  java
  • android 为TextView添加边框

     

     

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

    方法一:

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

    方法二:

    通过shape来设置背景图片

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

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


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

    android:background="@drawable/textview_border"  

    效果图片如下:


    方法三:

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

    1. package com.example.test;  
    2.   
    3. import android.annotation.SuppressLint;  
    4. import android.content.Context;  
    5. import android.graphics.Canvas;  
    6. import android.graphics.Paint;  
    7. import android.util.AttributeSet;  
    8. import android.widget.TextView;  
    9.   
    10. @SuppressLint("DrawAllocation")  
    11. public class BorderTextView extends TextView{  
    12.   
    13.     public BorderTextView(Context context) {  
    14.         super(context);  
    15.     }  
    16.     public BorderTextView(Context context, AttributeSet attrs) {  
    17.         super(context, attrs);  
    18.     }  
    19.     private int sroke_width = 1;  
    20.     @Override  
    21.     protected void onDraw(Canvas canvas) {  
    22.         Paint paint = new Paint();  
    23.         //  将边框设为黑色  
    24.         paint.setColor(android.graphics.Color.BLACK);  
    25.         //  画TextView的4个边  
    26.         canvas.drawLine(0, 0, this.getWidth() - sroke_width, 0, paint);  
    27.         canvas.drawLine(0, 0, 0, this.getHeight() - sroke_width, paint);  
    28.         canvas.drawLine(this.getWidth() - sroke_width, 0, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);  
    29.         canvas.drawLine(0, this.getHeight() - sroke_width, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);  
    30.         super.onDraw(canvas);  
    31.     }  
    32. }  


    效果图如下:



    使用的Xml布局内容如下:

     

      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:layout_width="match_parent"  
      4.     android:layout_height="match_parent" >  
      5.       
      6.     <TextView   
      7.         android:layout_width="120dp"  
      8.         android:layout_height="80dp"  
      9.         android:background="@drawable/textview_border"  
      10.         android:text="方法二"  
      11.         android:textColor="#FF000000"  
      12.         android:id="@+id/test"  
      13.         android:gravity="center"  
      14.         android:layout_alignParentTop="true"  
      15.         android:layout_marginTop="20dp"  
      16.         android:layout_centerHorizontal="true"  
      17.         />  
      18.   
      19.     <com.example.test.BorderTextView  
      20.          android:layout_width="120dp"  
      21.         android:layout_height="80dp"  
      22.          android:text="方法三"  
      23.          android:id="@+id/test3"  
      24.          android:gravity="center"  
      25.          android:layout_alignParentBottom="true"  
      26.          android:layout_marginBottom="20dp"  
      27.           android:layout_centerHorizontal="true"  
      28.         ></com.example.test.BorderTextView>  
      29. </RelativeLayout
  • 相关阅读:
    Markdown快捷笔记
    Linux常用命令
    Git使用
    HTML
    JavaScript-笔记2
    AngularJS-笔记2
    AngularJS-笔记1
    JQuery-笔记
    设置DataGridView的某个单元格为ComboBox
    记录文件浏览历史路径
  • 原文地址:https://www.cnblogs.com/xgjblog/p/3895005.html
Copyright © 2011-2022 走看看