溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spinner控件的列表項邊框圓角處理

發(fā)布時間:2024-08-19 09:09:30 來源:億速云 閱讀:84 作者:小樊 欄目:移動開發(fā)

要給Spinner控件的列表項添加圓角邊框,可以通過自定義Adapter來實現(xiàn)。首先,創(chuàng)建一個自定義的Adapter類,繼承自ArrayAdapter,并重寫getView()方法來設(shè)置列表項的樣式。

在getView()方法中,可以使用ShapeDrawable和GradientDrawable來繪制圓角邊框。具體步驟如下:

  1. 創(chuàng)建一個ShapeDrawable對象,并設(shè)置圓角半徑
ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(new float[]{10, 10, 10, 10, 10, 10, 10, 10}, null, null));
  1. 創(chuàng)建一個GradientDrawable對象,并設(shè)置填充顏色和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設(shè)置為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設(shè)置為列表項的背景
view.setBackground(shapeDrawable);

最后,將自定義的Adapter設(shè)置給Spinner控件即可實現(xiàn)列表項的圓角邊框效果。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI