这是 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/