Android set radius programatically to ShapeDrawable

﹥>﹥吖頭↗ 提交于 2019-12-13 11:13:22

问题


Can I set a radius programmatically to a ShapeDrawable?


回答1:


You can do it like this:

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.setBackgroundDrawable(shape);
}

You can use this function throughout your app and can put border and background color of your choice.




回答2:


try this:

        LayerDrawable bgDrawable = (LayerDrawable) btnCallnow.getBackground();
        final GradientDrawable shape = (GradientDrawable) bgDrawable.findDrawableByLayerId(R.id.shape_id);
        shape.setCornerRadius(5.0f);


来源:https://stackoverflow.com/questions/43039433/android-set-radius-programatically-to-shapedrawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!