Android adds a border to TextView

Source: Internet
Author: User

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
  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>


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
  1. Package com.example.test;
  2. Import Android.annotation.SuppressLint;
  3. Import Android.content.Context;
  4. Import Android.graphics.Canvas;
  5. Import Android.graphics.Paint;
  6. Import Android.util.AttributeSet;
  7. Import Android.widget.TextView;
  8. @SuppressLint ("drawallocation")
  9. Public class Bordertextview extends textview{
  10. Public Bordertextview (context context) {
  11. super (context);
  12. }
  13. Public Bordertextview (context context, AttributeSet attrs) {
  14. Super (context, attrs);
  15. }
  16. private int sroke_width = 1;
  17. @Override
  18. protected void OnDraw (canvas canvas) {
  19. Paint paint = new paint ();
  20. //Set the border to black
  21. Paint.setcolor (Android.graphics.Color.BLACK);
  22. //Draw 4 sides of a TextView
  23. Canvas.drawline (0, 0, this.getwidth ()-Sroke_width, 0, paint);
  24. Canvas.drawline (0, 0, 0, this.getheight ()-sroke_width, paint);
  25. Canvas.drawline (this.getwidth ()-Sroke_width, 0, this.getwidth ()-Sroke_width, this.getheight ()-  Sroke_width, paint);
  26. Canvas.drawline (0, this.getheight ()-Sroke_width, this.getwidth ()-Sroke_width, this.getheight ()-  Sroke_width, paint);
  27. Super.ondraw (canvas);
  28. }
  29. }


As follows:



The following XML layout is used:

[HTML]View Plaincopy
    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. <TextView
    6. android:layout_width="120DP"
    7. android:layout_height="80DP"
    8. android:background="@drawable/textview_border"
    9. android:text="method two"
    10. android:textcolor="#FF000000"
    11. android:id="@+id/test"
    12. android:gravity="center"
    13. android:layout_alignparenttop="true"
    14. android:layout_margintop="20DP"
    15. android:layout_centerhorizontal="true"
    16. />
    17. <Com.example.test.BorderTextView
    18. android:layout_width="120DP"
    19. android:layout_height="80DP"
    20. android:text="method three"
    21. android:id="@+id/test3"
    22. android:gravity="center"
    23. android:layout_alignparentbottom="true"
    24. android:layout_marginbottom="20DP"
    25. android:layout_centerhorizontal="true"
    26. ></com.example.test.BorderTextView>
    27. </relativelayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.