android - 如何在Android Kotlin中根据其宽度将圆角应用于视图
问题描述
我正在制作一个自定义进度条,如下图所示:
基本上,我创建了一个可绘制的 xml 背景文件:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/jungleGreen"/>
<corners
android:bottomLeftRadius="40dp"
android:topLeftRadius="40dp"/>
</shape>
然后我将它应用于我正在使用的视图:
<View
android:id="@+id/progress_bar_placeholder_view"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginEnd="45dp"
android:background="@drawable/background_filled_patronage_progressbar"
app:layout_constraintTop_toTopOf="parent"/>
完全没问题,我可以实现场景 1 和 2,但是当栏越来越接近末端时,如何以编程方式设置视图右上角和右下角的圆角,直到看起来像照片3?
谢谢。
解决方案
试试这个
public static void customView(View v, int backgroundColor, int borderColor)
{
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
shape.setColor(backgroundColor);
shape.setStroke(3, borderColor);
v.setBackground(shape);
}
推荐阅读
- angular - Angular 英雄之旅大师详细解说
- javascript - 基于 ID 是否在嵌套对象中从 JSON 对象访问值
- excel-formula - 如何在 Excel 中使用 IF 函数查找简单的嵌入项
- c# - 按钮 performclick 没有做任何事情
- node.js - NodeJS UDP 一台服务器多客户端和端口,有助于理解端口行为
- android - 颤振 MissingPluginException
- maven - Maven部署两次
- r - 使用.data.table 观察到的去年(半年、月)的平均值
- postgresql - 在 postgresql 中旋转大表
- php - 如何允许 php(apache 作为 www-data 用户)执行命令?