view设置边框大小和颜色
时间: 2023-08-02 08:11:04 浏览: 61
要设置视图(View)的边框大小和颜色,你可以使用以下方法:
1. 使用XML布局文件:
在XML布局文件中,为视图设置以下属性来定义边框大小和颜色:
```xml
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:padding="5dp" />
```
在上面的代码中,`@drawable/border` 是一个自定义的Drawable资源,用于定义边框的样式。你可以在项目的`res/drawable`目录下创建一个XML文件,命名为`border.xml`,并添加以下代码:
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="2dp"
android:color="#FF0000" />
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
</shape>
```
在上面的代码中,`android:width` 属性定义了边框的大小,`android:color` 属性定义了边框的颜色。
2. 使用代码设置:
在Java代码中,你可以使用以下方法来设置视图的边框大小和颜色:
```java
View view = findViewById(R.id.view_id);
GradientDrawable border = new GradientDrawable();
border.setStroke(2, Color.RED); // 设置边框的大小和颜色
view.setBackground(border);
```
在上面的代码中,`2` 表示边框的大小,`Color.RED` 表示边框的颜色。
希望对你有所帮助!如有任何问题,请随时提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![vsdx](https://img-home.csdnimg.cn/images/20210720083646.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)