android 為TextView添加邊框


今天需要在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;  
        @Override  
        protected 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>  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM