android 修改shape solid颜色
时间: 2023-08-10 18:38:38 浏览: 55
要修改 Android 中的 shape 的 solid 颜色,可以在 shape 文件中找到 solid 标签,并修改其对应的颜色值。例如,如果你想将红色改为蓝色,可以将以下代码中的 #FF0000 替换为 #0000FF:
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF0000" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
```
修改后的代码如下:
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#0000FF" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
```
这样就将 shape 的 solid 颜色修改为了蓝色。
相关问题
android 代码修改shape solid颜色
在 Android 中,除了在 xml 文件中修改 shape 的 solid 颜色,也可以在代码中进行修改。具体方法是通过获取 shape 对应的 GradientDrawable 对象,然后调用其 setSolid() 方法来设置颜色。以下是示例代码:
```java
// 获取 shape 对应的 GradientDrawable 对象
GradientDrawable shape = (GradientDrawable) context.getDrawable(R.drawable.shape);
// 设置 solid 颜色
shape.setSolid(Color.BLUE);
// 将修改后的 shape 应用到指定的 View 上
view.setBackground(shape);
```
其中,R.drawable.shape 表示 shape 的资源 ID,可以根据实际情况进行修改。上述代码中将 solid 颜色设置为蓝色,你也可以根据需要设置其他颜色。最后,通过 setBackground() 方法将修改后的 shape 应用到指定的 View 上即可。
Android全局动态修改shape颜色
可以通过代码动态修改Shape的颜色,以下是一个示例:
1. 定义一个Shape Drawable文件,例如shape.xml:
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="@color/colorPrimary" />
</shape>
```
2. 在代码中获取Shape Drawable并设置颜色:
```java
GradientDrawable shapeDrawable = (GradientDrawable) getResources().getDrawable(R.drawable.shape);
shapeDrawable.setColor(getResources().getColor(R.color.colorAccent));
```
这里将Shape的颜色设置为R.color.colorAccent。如果需要动态修改Shape的边框颜色等属性,也可以通过类似的方式进行设置。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)