android 為TextView添加邊框

來源:互聯網
上載者:User

今天需要在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>




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.