Bordertextviews.java
[Java]View PlainCopy
- Package Xiaosi. Bordertextview;
- Import Android.content.Context;
- Import Android.graphics.Canvas;
- Import Android.graphics.Color;
- Import Android.graphics.Paint;
- Import Android.util.AttributeSet;
- Import Android.widget.TextView;
- Public class Bordertextviews extends TextView
- {
- Private Paint paint = null;
- private int color = Color.gray;
- Public bordertextviews (context context, AttributeSet attrs)
- {
- Super (context, attrs);
- }
- //Set Border color
- public void Setpaintcolor (int color) {
- this.color = color;
- }
- @Override
- protected void OnDraw (canvas canvas)
- {
- Super.ondraw (canvas);
- Paint = new paint ();
- //Set color for border
- Paint.setcolor (color);
- //On
- Canvas.drawline (0, 0, this.getwidth ()-1, 0, paint);
- //Left
- Canvas.drawline (0, 0, 0, this.getheight ()-1, paint);
- //Down
- Canvas.drawline (0, this.getheight ()-1, this.getwidth ()-1, this.getheight ()-1, paint);
- //Right
- Canvas.drawline (this.getwidth ()-1, 0, this.getwidth ()-1, this.getheight ()-1, Paint);
- }
- }
[Java]View PlainCopy
- Package Xiaosi. Bordertextview;
- Import android.app.Activity;
- Import Android.graphics.Color;
- Import Android.os.Bundle;
- Public class Bordertextviewactivity extends Activity {
- /** Called when the activity is first created. * /
- private Bordertextviews Bordertextview = null;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Bordertextview = (bordertextviews) Findviewbyid (R.id.border);
- Bordertextview.setpaintcolor (Color.gray);
- }
- }
Main.xml
[Java]View PlainCopy
- <?xml version="1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
- Android:layout_width="Fill_parent"
- android:layout_height="Fill_parent"
- android:orientation="Vertical"
- android:background="#CCFF66" >
- <xiaosi. Bordertextview.bordertextviews
- android:id="@+id/border"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:textcolor="#C71585"
- android:layout_margintop="20DP"
- android:padding="10DP"
- android:layout_gravity="center"
- android:text="Draw a border on canvas"/>
- </LinearLayout>
Text plus border for Android learning note tips