歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android開發之自定義帶邊框的TextView

自定義帶邊框的TextView

///////////////////////Activity///////////////////////////////

package cn.class3g.activity;

 

import Android.content.Context;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.widget.TextView;

 

public class MyBorderTextView extends TextView{

 

      public MyBorderTextView(Context context, AttributeSet attrs) {

           super(context, attrs);

          

      }

      @Override

      protected void onDraw(Canvas canvas) {

           super.onDraw(canvas);

          

           Paint paint = new Paint();

          

           paint.setColor(android.graphics.Color.YELLOW);

          

 

           canvas.drawLine(0, 0, this.getWidth()-1, 0, paint);

//1、橫坐標0到this.getWidth()-1,縱坐標0到0

           canvas.drawLine(0, 0, 0, this.getHeight()-1, paint);

//2、橫坐標0到0,縱坐標0到this.getHeight()-1

           canvas.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()-1, paint);

//3、橫坐標this.getWidth()-1到this.getWidth()-1,縱坐標0到this.getHeight()-1

           canvas.drawLine(0, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, paint);

//4、橫坐標0到this.getWidth()-1,縱坐標this.getHeight()-1到this.getHeight()-1

//下面用圖介紹邊框的繪制

      }

 

}

1

this.getWidth,this.getHeight

0,this.getHeight

this.getWidth()-1,0

0,0

邊框的繪制

3

4

2

 

 

 


然後只需要在布局裡調用這個就行

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

 

    <cn.class3g.activity.MyBorderTextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_margin="10dp"

        android:padding="30dp"

        android:text="hello"

        android:textColor="#cccccc" >

    </cn.class3g.activity.MyBorderTextView>

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_margin="10dp"

        android:padding="30dp"

        android:text="hello hello hello hello"

        android:background="@drawable/ic_launcher"

        android:textColor="#cccccc" >

    </TextView>

 

</LinearLayout>

效果圖:

 

Copyright © Linux教程網 All Rights Reserved