繁体   English   中英

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

[英]How to change the stroke width of a shape programmatically in Android?

这是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并将其DrawableGradientDrawable

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

3)使用setStroke()方法将其应用于一个笔划setStroke()以像素和颜色传递宽度):

backgroundGradient.setStroke(5, Color.BLACK);

整码:

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

您可能需要以编程方式创建它

ShapeDrawable circle = new ShapeDrawable( new  OvalShape() );

你需要在此之后设置属性,(填充,颜色等)然后改变它的笔划

circle.getPaint().setStrokeWidth(12);

然后将其设置为视图的背景

textview.setBackgroundDrawable(circle);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM