自定义圆角背景的textview,抛弃shape
自定义一个圆角背景的TextView,解放双手,不用再写shape了。
1.values目录新建attrs.xml。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="test">
<attr name="radius" format="integer" />
<attr name="stokeWidth" format="float" />
<attr name="stokeColor" format="reference|color"/>
<attr name="solidColor" format="reference|color"/>
</declare-styleable>
</resources>
2.新建类继承TextView。
public class CustomRoundTextView extends AppCompatTextView {
private int solidColor;
private int stokeColor;
private float stokeWidth;
private int cornerRadius;
private Paint mPaint;
public CustomRoundTextView(Context context) {
super(context);
init();
}
public CustomRoundTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.test);
cornerRadius = typedArray.getInteger(R.styleable.test_radius, 20);
stokeWidth = typedArray.getFloat(R.styleable.test_stokeWidth, 0.5f);
solidColor = typedArray.getColor(R.styleable.test_solidColor, getResources().getColor(android.R.color.white,null));
stokeColor = typedArray.getColor(R.styleable.test_stokeColor,solidColor );
init();
}
public CustomRoundTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(dip2px(cornerRadius));
gd.setStroke(dip2px(stokeWidth), stokeColor);
gd.setColor(solidColor);
setBackgroundDrawable(gd);
}
public int dip2px(float dpValue) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
3.xml文件中使用。
<com.example.myapplication.CustomRoundTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="16sp"
android:text="这是一个test"
android:textColor="#000000"
app:radius="5"
app:stokeWidth="0.5"
app:stokeColor="#000000"
app:solidColor="#ff00000"
/>
4.圆形自定义也可以拓展实现。
效果实现如下:

自定义view相关博客:
2.自定义控件
自定义圆角背景的textview,抛弃shape的更多相关文章
- 【Android UI】案例02 圆角边框、圆角背景的实现(shape)
本文主要分享圆角边框与圆角背景的实现方式.该方式的实现,须要了解shape的使用.该部分的具体介绍,请阅读博客http://blog.csdn.net/mahoking/article/details ...
- Android Imageview 图片居左居右,自定义圆角
android:scaleType="fitStart" 图片靠左不变形显示, android:scaleType=”fitEnd” 图片靠右显示,不变形. 半透明andr ...
- 自定义圆角透明的Dialog
自定义圆角透明的Dialog 说明 系统默认的Dialog默认是背景不透明的,有时候项目需要Dialog为圆角透明,这个时候的解决方案就是---重写Dialog - 系统样式 - 自定义以后的样式 自 ...
- NEC学习 ---- 模块 -文本圆角背景导航
下图是效果图: 然后, 左右两边的圆角图片和背景图片如下 (因为截图工具的原因, 可能图片不是很清晰. 这个图片有4个部分, 分别是中间的背景图, 左右圆角以及栏目分隔白线) 思路: 利用inline ...
- HTML5 Canvas自定义圆角矩形与虚线(Rounded Rectangle and Dash Line)
HTML5 Canvas自定义圆角矩形与虚线(RoundedRectangle and Dash Line) 实现向HTML Canvas 2d context绘制对象中添加自定义的函数功能演示,如何 ...
- MFC圆角背景移动边角底色毛刺解决方案
CRect rc; Graphics graphics(pDC->m_hDC); GetClientRect(&rc); CRgn m_rgn; if (m_pBgImage) { gr ...
- 基于HTML5自定义文字背景生成QQ签名档
分享一款利用HTML5实现的自定义文字背景应用,首先我们可以输入需要显示的文字,并且为该文字选择一张背景图片,背景图片就像蒙版一样覆盖在文字上.点击生成QQ签名档即可将文字背景融为一体生成另外一张图片 ...
- (转)Android 自定义 spinner (背景、字体颜色)
Android 自定义 spinner (背景.字体颜色) (2012-07-04 17:04:44) 1.准备两张图片,并做好9.png 2.在drawable中定义spinner_sele ...
- Android自定义圆角矩形进度条2
效果图: 或 方法讲解: (1)invalidate()方法 invalidate()是用来刷新View的,必须是在UI线程中进行工作.比如在修改某个view的显示时, 调用invalidate()才 ...
随机推荐
- k8s集群,使用pvc方式实现数据持久化存储
环境: 系统 华为openEulerOS(CentOS7) k8s版本 1.17.3 master 192.168.1.244 node1 192.168.1.245 介绍: 在Kubernetes中 ...
- MeteoInfo脚本示例:读取FY3A AOD HDF文件
FY3A卫星有AOD产品数据,HDF格式,这里示例用MeteoInfo脚本程序读取和显示该类数据. 脚本程序如下: #----------------------------------------- ...
- spring boot:方法中使用try...catch导致@Transactional事务无效的解决(spring boot 2.3.4)
一,方法中使用try...catch导致@Transactional事务无效的解决方法 1,问题的描述: 如果一个方法添加了@Transactional注解声明事务, 而方法内又使用了try catc ...
- rabbitmq 延时队列
前言 某个产品 或者订单,有个有效期 过了有效期要取消 方法一 : 写个脚本,用crontab 定时扫描 改变状态 但是最低只能一分钟 ,不适合 方法二 : 用swoole得毫秒定时器,每秒钟去扫描表 ...
- Linux-京西百花山
百花山有三个收票的入口,分别在门头沟(G109).房山(G108)和河北 108有两个方向上百花山,史家营和四马台.只有史家营方向能开车到山顶. 四马台那边,不住,要坐景区车才行 尽头是1900多米的 ...
- 如何解决 An error occured executing the Microsoft VC+runtime installer
安装 postgresql 时遇见了 这个问题 There has been an error.An error occured executing the Microsoft VC+ runtim ...
- jQuery load() 中文乱码
1.使用editplus创建了demo.txt (ANSI保存的), $("#div1").load("demo.txt");//div显示中文乱码---> ...
- Go 包管理历史以及 Go mod 使用
之前也写过 Go 管理依赖工具 godep 的使用,当时看 godep 使用起来还是挺方便,其原因主要在于有总比没有强.关于依赖管理工具其实还是想从头聊聊这个需求以及大家做这个功能的各种出发点. GO ...
- better-scroll插件 api
Vue中的better-scroll插件 在需要的文件中添加 import BScorll from 'better-scroll'; 引用的示例代码: let scroll = new BScrol ...
- 10 Servlet_02 资源跳转(主要是内部转发)与中文乱码问题
总的知识点: 1.小的知识点总结: alt + shift + r 重命名快捷键(可以给包和类以及项目重命名) 有序列表 ol li 无序列表 ul type 格式 text 是文本类型 passwo ...