你好,游客 登录 注册 搜索
背景:
阅读新闻

Android应用技巧之给文本加边框

[日期:2012-02-25] 来源:Linux社区  作者:sjf0115 [字体:大 中 小]

BorderTextViews.java

[java]
  1. package xiaosi.BorderTextView;  
  2.   
  3. import Android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.util.AttributeSet;  
  8. import android.widget.TextView;  
  9.   
  10. public class BorderTextViews extends TextView  
  11. {  
  12.     private Paint paint = null;  
  13.     private int color = Color.GRAY;  
  14.     public BorderTextViews(Context context, AttributeSet attrs)  
  15.     {  
  16.         super(context, attrs);  
  17.     }  
  18.     //设置边框颜色   
  19.     public void setPaintColor(int color){  
  20.         this.color = color;  
  21.     }  
  22.     @Override  
  23.     protected void onDraw(Canvas canvas)  
  24.     {  
  25.         super.onDraw(canvas);  
  26.         paint = new Paint();  
  27.         //给边框设置颜色   
  28.         paint.setColor(color);  
  29.         //上   
  30.         canvas.drawLine(0, 0, this.getWidth()-1, 0, paint);  
  31.         //左   
  32.         canvas.drawLine(0, 0, 0, this.getHeight()-1, paint);  
  33.         //下   
  34.         canvas.drawLine(0, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, paint);  
  35.         //右   
  36.         canvas.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()-1, paint);  
  37.     }  
  38. }  

[java]

  1. package xiaosi.BorderTextView;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6.   
  7. public class BorderTextViewActivity extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     private BorderTextViews borderTextView = null;  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.         borderTextView = (BorderTextViews)findViewById(R.id.Border);  
  15.         borderTextView.setPaintColor(Color.GRAY);  
  16.     }  
  17. }  

[java]

  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.   
  8.     <xiaosi.BorderTextView.BorderTextViews  
  9.         android:id="@+id/Border"  
  10.         android:layout_width="wrap_content"   
  11.         android:layout_height="wrap_content"  
  12.         android:textColor="#C71585"   
  13.         android:layout_marginTop="20dp"  
  14.         android:padding="10dp"   
  15.         android:layout_gravity="center"   
  16.         android:text="在画布上画边框" />  
  17.   
  18. </LinearLayout>  
linux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款