博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android学习笔记技巧之给文本加边框
阅读量:7109 次
发布时间:2019-06-28

本文共 1995 字,大约阅读时间需要 6 分钟。

BorderTextViews.

[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. }  

 

main.xml

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

 

转载地址:http://salhl.baihongyu.com/

你可能感兴趣的文章
Confluence 6 从 Crowd 或 JIRA 应用中切换回使用内部用户管理
查看>>
Python全栈 Web(jQuery 一条龙服务)
查看>>
每日文献:2018-01-29
查看>>
Stack Overflow 那些让人头大的规矩
查看>>
Python网易云音乐爬虫进阶篇
查看>>
WPS Office 2019 上架微软商城,全新可定制 UI
查看>>
高性能网络IO模型
查看>>
REST表述性状态传递
查看>>
服务器宕机原因
查看>>
Spring Cloud之极端续租间隔时间的影响
查看>>
Android 输入金额限制,各种限制~
查看>>
微软职位内部推荐-SW Engineer II for Skype
查看>>
用代码构建机器心智,我们离这个目标还有多远?
查看>>
ASP.net防止SQL注入方法
查看>>
「mysql优化专题」视图应用竟然还可以这么优化?不得不收藏(8)
查看>>
聚能聊每周精选 第十四期
查看>>
Android面试之Java基础
查看>>
React 16 Jest定时器模拟 Timer Mocks
查看>>
《Kotin 极简教程》第7章 面向对象编程(OOP)(2)
查看>>
Facebook开源看图问答模型Pythia:拿下VQA比赛冠军就靠它
查看>>