java - 如何在 Android 中以编程方式更改形状的笔划宽度?

标签 java android shapes

这是 circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <solid android:color="#00000000"/> 
    <padding android:left="30dp" android:top="30dp"
             android:right="30dp" android:bottom="30dp" />
    <stroke android:color="#439CC8" android:width="7dp" />
</shape>

这是我的代码:

textview.setBackgroundResource(R.drawable.circle);

我想在我的 java 代码中更改笔划粗细。如何以编程方式更改它?

最佳答案

这样做:

1) 使用通常的 findViewById() 获取 TextView :

TextView textView = (TextView) rootView.findViewById(R.id.resourceName);

2) 使用 getBackground()TextView 获取 Drawable 并将其转换为 GradientDrawable:

GradientDrawable backgroundGradient = (GradientDrawable) textView.getBackground();

3) 使用 setStroke() 方法给它画笔画(给它传递像素宽度和颜色):

backgroundGradient.setStroke(5, Color.BLACK);

完整代码:

TextView textView = (TextView) rootView.findViewById(R.id.resourceName);
GradientDrawable backgroundGradient = (GradientDrawable) textView.getBackground();
backgroundGradient.setStroke(5, Color.BLACK);

关于java - 如何在 Android 中以编程方式更改形状的笔划宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27492401/

相关文章:

android - 我想创建自定义的 android 生日祝福应用程序

html - 如何让文字正常变形(平行四边形)?

javascript - Canvas :将圆拆分为水平条

java - 任务 ':app:processDebugGoogleServices' Firebase&Android Studio 执行失败

java - SWT - ScrolledComposite 内多行文本字段的 computingSize

java - Maven war 插件出现格式错误的 pom 错误

java - 使用Java ProcessBuilder调用全局安装的 Node 模块

android - 如何使用 ConstraintLayout 水平对齐三个 TextView

android - 是否有任何 Android 函数调用只进行一两个特定的系统调用?

html - 从 CSS 内置的标签形状中删除白色背景