Today need to add a border above the TextView, but the TextView itself does not support borders, so can only be used in other ways, on-line query, There are three main ways to achieve 1. Transparent picture with Border 2. Use the shape settings of XML 3 to inherit the TextView overwrite OnDraw method.
Method One:
With a transparent picture of the background, this is nothing to be done, make one on your own, and then set background on it.
Method Two:
Setting a background image with shape
First, a textview_border.xml file is placed in the Drawable folder .
[HTML]View Plaincopy
- <? 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>
Add a background for the textview you want to add a border to
android:background= "@drawable/textview_border"
The film is as follows:
Method Three:
Write a custom component that inherits the TextView class and draw a border in the OnDraw event method.
[Java]View Plaincopy
- 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 ();
- //Set the border to black
- Paint.setcolor (Android.graphics.Color.BLACK);
- //Draw 4 sides of a TextView
- 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);
- }
- }
As follows:
The following XML layout is used:
[HTML]View Plaincopy
- <? 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="method two"
- 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="method three"
- android:id="@+id/test3"
- android:gravity="center"
- android:layout_alignparentbottom="true"
- android:layout_marginbottom="20DP"
- android:layout_centerhorizontal="true"
- ></com.example.test.BorderTextView>
- </relativelayout>