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/

相关文章:

java - 如何使用 Java SWT .setBackground() 方法

java - 使用Andengine画圆

java - JNLP 在加载时未正确加载

java - 是否有与 unputc 等效的 Java?

android.widget.LinearLayout$LayoutParams 无法转换为 android.support.constraint.ConstraintLayout$LayoutParams

android - Ionic4 电容 android livereload?

Android Things OTA 流程

html - CSS3 箭头不显示

java - 绘制任意边的形状

java - 从另一个 Java 控制台获取我的应用程序的输出