在Android中,可以通过以下几种方式来绘制圆角边框:
1. 使用ShapeDrawable:
首先,创建一个shape.xml文件,并在其中定义一个圆角矩形。
```xml
android:color="#ffff00" android:width="5dp" /> ``` 然后,将shape.xml文件作为背景设置给你的View: ```xml android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/shape" /> ``` 2. 使用GradientDrawable: 在代码中创建一个GradientDrawable对象,并设置其圆角半径、背景颜色和边框宽度。 ```java GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setColor(Color.RED); // 设置填充颜色 gradientDrawable.setCornerRadius(10); // 设置圆角半径 gradientDrawable.setStroke(5, Color.YELLOW); // 设置边框宽度和颜色 View view = findViewById(R.id.view); view.setBackground(gradientDrawable); ``` 3. 使用CardView: CardView是Android Support Library中提供的一个布局容器,它可以自动为内容添加圆角边框。 ```xml android:layout_width="100dp" android:layout_height="100dp" app:cardCornerRadius="10dp" app:cardBackgroundColor="#FF0000" app:cardElevation="5dp" app:cardStrokeColor="#ffff00" app:cardStrokeWidth="5dp" /> ``` 以上是三种常用的在Android中绘制圆角边框的方法。根据你的需求和实际情况,选择适合的方式即可。