温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spinner控件的列表项边框圆角处理

发布时间:2024-08-19 09:09:30 来源:亿速云 阅读:83 作者:小樊 栏目:移动开发

要给Spinner控件的列表项添加圆角边框,可以通过自定义Adapter来实现。首先,创建一个自定义的Adapter类,继承自ArrayAdapter,并重写getView()方法来设置列表项的样式。

在getView()方法中,可以使用ShapeDrawable和GradientDrawable来绘制圆角边框。具体步骤如下:

  1. 创建一个ShapeDrawable对象,并设置圆角半径
ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(new float[]{10, 10, 10, 10, 10, 10, 10, 10}, null, null));
  1. 创建一个GradientDrawable对象,并设置填充颜色和ShapeDrawable作为背景
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.WHITE);
gd.setStroke(2, Color.GRAY);
gd.setShape(GradientDrawable.RECTANGLE);
gd.setCornerRadii(new float[]{10, 10, 10, 10, 10, 10, 10, 10});
gd.setCornerRadii(new float[]{10, 10, 10, 10, 10, 10, 10, 10});
shapeDrawable.setPadding(5, 5, 5, 5);
shapeDrawable.getPaint().setColor(Color.WHITE);
shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
shapeDrawable.getPaint().setStrokeWidth(2);
shapeDrawable.setIntrinsicWidth(50);
shapeDrawable.setIntrinsicHeight(50);
gd.setShape(GradientDrawable.RECTANGLE);
gd.setCornerRadius(10);
gd.setStroke(2, Color.GRAY);
gd.setShape(GradientDrawable.RECTANGLE);
shapeDrawable.getPaint().setColor(Color.WHITE);
shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
shapeDrawable.getPaint().setStrokeWidth(2);
  1. 将GradientDrawable设置为ShapeDrawable的背景
shapeDrawable.getPaint().setStrokeWidth(2);
shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
shapeDrawable.getPaint().setColor(Color.GRAY);
shapeDrawable.getPaint().setStrokeWidth(2);
shapeDrawable.setShape(new RoundRectShape(new float[]{10, 10, 10, 10, 10, 10, 10, 10}, null, null));
  1. 将ShapeDrawable设置为列表项的背景
view.setBackground(shapeDrawable);

最后,将自定义的Adapter设置给Spinner控件即可实现列表项的圆角边框效果。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI