当前位置:网站首页>控件 圆角描边 MaterialShapeDrawable
控件 圆角描边 MaterialShapeDrawable
2022-07-28 21:54:00 【锐湃】
最近UI 一直出现不统一的背景色,不同的圆角,四个角度不同背景描边都不同,我只能不停的去写drawable布局,稍微有点不同就是就去创建不同的drawable,巨烦.所以写了这了一个工具类,永远解决不同UI控件的问题.
不说废话 直接上图:

引用
implementation "com.google.android.material:material:1.3.0"
上代码 代码是用kotlin 顶层方法写的 Java与 kotlin 都可以正常调用
/**
* 控件圆角
* @param Context 上下文
* @param fillColor 填充颜色
* @param stroke 描边宽度
* @param colorRes //描边颜色
* @param allCorner 四边角度
* @param topLeftCorner 左上角
* @param topRightCorner 右上角
* @param bottomRightCorner 左下角
* @param bottomLeftCorner 右下角
*/
@JvmOverloads
fun roundedCorners(activity: Context, fillColor: Int, stroke: Float, colorRes: Int, allCorner: Float,
topLeftCorner: Float = 0f, topRightCorner: Float = 0f, bottomRightCorner: Float = 0f, bottomLeftCorner: Float = 0f): MaterialShapeDrawable {
val shapeAppearanceModel2 = ShapeAppearanceModel.builder().apply {
setAllCorners(RoundedCornerTreatment())
//setAllCornerSizes(allCorner)
setTopLeftCornerSize(if (topLeftCorner == 0f) allCorner else topLeftCorner)
setTopRightCornerSize(if (topRightCorner == 0f) allCorner else topRightCorner)
setBottomRightCornerSize(if (bottomRightCorner == 0f) allCorner else bottomRightCorner)
setBottomLeftCornerSize(if (bottomLeftCorner == 0f) allCorner else bottomLeftCorner)
}.build()
val drawable2: MaterialShapeDrawable = MaterialShapeDrawable(shapeAppearanceModel2).apply {
setTint(ContextCompat.getColor(activity, fillColor)) //填充颜色
paintStyle = Paint.Style.FILL_AND_STROKE
strokeWidth = stroke//描边宽度
strokeColor = ContextCompat.getColorStateList(activity, colorRes)//描边颜色
}
return drawable2
}
kotlin 调用代码:
//四边圆80f
aBinding.background = roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f);
//左上边圆80f
bBinding.background = roundedCorners(this,R.color.light_gray,20f,R.color.red_ff4500,0f,80f);
//右上边圆80f
cBinding.background = roundedCorners(this,R.color.red_ff4500,30f,R.color.button_2a73fa,0f,topRightCorner=80f);
//左下边圆80f
dBinding.background = roundedCorners(this,R.color.red_ff4500,5f,R.color.button_2a73fa,0f,bottomRightCorner=80f);
//右下边圆80f
eBinding.background = roundedCorners(this,R.color.colorPrimary,2f,R.color.colorAccent,0f,bottomLeftCorner=80f);
Java 代码调用
Java没有kotlin调用这种多参方便, 没办法指定传那个参数,如果想改变最后一个角就必须把前面的也都传值.
ControlsTheRoundedKt 是顶层方法就是上面的 roundedCorners代码名字
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f));
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f,20f));
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f,20f,10f));
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f,20f,10f,20f));
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f,20f,10f,20f,30f));搞定是不是非常方便,再也不用一直创建drawable写布局了
————————————————
版权声明:本文为CSDN博主「软件熊猫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Originality_special/article/details/118222330
边栏推荐
- 2022焊工(初级)上岗证题目及答案
- Shenkaihong: on the river of Zhilian of all things, there is a bright moon of open source
- Asynchronism and synchronization of visa write and read functions by LabVIEW
- Arduino uno driver universe 1.8 'TFT SPI screen example demonstration (including data package)
- 二舅火了,全网刷屏,他凭什么能治好我的精神内耗?
- 2022G3锅炉水处理考试模拟100题模拟考试平台操作
- [self] - brush questions array
- 【自】-刷题-BFS
- Runloop principle (II)
- 被忽视的智能电视小程序领域
猜你喜欢

Shenkaihong: on the river of Zhilian of all things, there is a bright moon of open source

事件抽取文献整理(2008-2017)

Mycms we media mall V3.6 release, compatible with micro engine application development (laravel framework)

Zero vision technology completed the pre-A round of financing and promoted the domestic replacement of intelligent driving platform software
![Trivy [2] tool vulnerability scanning](/img/7a/c1012c75b23076f5a9b09e5756adba.png)
Trivy [2] tool vulnerability scanning

Crud of MySQL

What if win11 quick copy and paste cannot be used? Win11 shortcut copy and paste cannot be used

MySQL transaction and storage system

编译原理研究性学习专题 2——递归下降语法分析设计原理与实现

Asynchronism and synchronization of visa write and read functions by LabVIEW
随机推荐
解决线程安全问题&&单例模式
String string
Samba service setup
事件抽取文献整理(2018)
Classic topological sorting problem -- leetcode207 curriculum +leetcode210 curriculum II
金仓数据库 KingbaseES 与 Oracle 的兼容性说明(3. 常用函数)
Function function
Trivy [2] tool vulnerability scanning
The classic dual stack implementation queue, pay attention to the modification of the judgment conditions of traversing the stack.
LabVIEW对VISA Write和Read函数的异步和同步
苹果官网正在更新维护 Apple Store,国行 iPhone 13 / Pro 等产品将最高优惠 600 元
2022g3 boiler water treatment test simulation 100 questions simulation test platform operation
Compatibility description between kingbasees and Oracle (3. Common functions)
金仓数据库 KingbaseES 与 Oracle 的兼容性说明(4. SQL)
trivy【3】自定义扫描策略
How does VR panorama entrepreneurship expand the market? How to make the road of entrepreneurship smoother?
深度剖析集成学习Xgboost
MySQL introduction
CV目标检测模型小抄(2)
KingbaseES客户端编程接口指南-ODBC(4. 创建数据源)