Text plus border for Android learning note tips

Source: Internet
Author: User

Bordertextviews.java

[Java]View PlainCopy
  1. Package Xiaosi. Bordertextview;
  2. Import Android.content.Context;
  3. Import Android.graphics.Canvas;
  4. Import Android.graphics.Color;
  5. Import Android.graphics.Paint;
  6. Import Android.util.AttributeSet;
  7. Import Android.widget.TextView;
  8. Public class Bordertextviews extends TextView
  9. {
  10. Private Paint paint = null;
  11. private int color = Color.gray;
  12. Public bordertextviews (context context, AttributeSet attrs)
  13. {
  14. Super (context, attrs);
  15. }
  16. //Set Border color
  17. public void Setpaintcolor (int color) {
  18. this.color = color;
  19. }
  20. @Override
  21. protected void OnDraw (canvas canvas)
  22. {
  23. Super.ondraw (canvas);
  24. Paint = new paint ();
  25. //Set color for border
  26. Paint.setcolor (color);
  27. //On
  28. Canvas.drawline (0, 0, this.getwidth ()-1, 0, paint);
  29. //Left
  30. Canvas.drawline (0, 0, 0, this.getheight ()-1, paint);
  31. //Down
  32. Canvas.drawline (0, this.getheight ()-1, this.getwidth ()-1, this.getheight ()-1, paint);
  33. //Right
  34. Canvas.drawline (this.getwidth ()-1, 0, this.getwidth ()-1, this.getheight ()-1,  Paint);
  35. }
  36. }

[Java]View PlainCopy
  1. Package Xiaosi. Bordertextview;
  2. Import android.app.Activity;
  3. Import Android.graphics.Color;
  4. Import Android.os.Bundle;
  5. Public class Bordertextviewactivity extends Activity {
  6. /** Called when the activity is first created. * /
  7. private Bordertextviews Bordertextview = null;
  8. @Override
  9. public void OnCreate (Bundle savedinstancestate) {
  10. super.oncreate (savedinstancestate);
  11. Setcontentview (R.layout.main);
  12. Bordertextview = (bordertextviews) Findviewbyid (R.id.border);
  13. Bordertextview.setpaintcolor (Color.gray);
  14. }
  15. }


Main.xml

[Java]View PlainCopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. Android:layout_width="Fill_parent"
  4. android:layout_height="Fill_parent"
  5. android:orientation="Vertical"
  6. android:background="#CCFF66" >
  7. <xiaosi. Bordertextview.bordertextviews
  8. android:id="@+id/border"
  9. Android:layout_width="Wrap_content"
  10. android:layout_height="Wrap_content"
  11. android:textcolor="#C71585"
  12. android:layout_margintop="20DP"
  13. android:padding="10DP"
  14. android:layout_gravity="center"
  15. android:text="Draw a border on canvas"/>
  16. </LinearLayout>

Text plus border for Android learning note tips

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.