【问题标题】:How to set shapedrawable's border and fill colours to different colours如何设置shapedrawable的边框和填充颜色为不同的颜色
【发布时间】:2018-06-18 10:45:12
【问题描述】:
我需要将 shapedrawable 的边框颜色和填充颜色设置为不同的颜色。如何设置两种颜色并将形状作为填充和描边?
说我有这个:
ShapeDrawable border = new ShapeDrawable(new RectShape());
border.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
border.getPaint().setColor(Color.WHITE);
border.getPaint().setStrokeColour(Color.BLACK); //???????? How to do
编辑:我的问题不是 Android ShapeDrawable set Background and Border programmatically 的重复,因为该问题的唯一答案不清楚并且对我没有帮助。
【问题讨论】:
标签:
java
android
android-studio
【解决方案1】:
您可能希望改为在 XML 中定义您的可绘制对象。它看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/WHITE"/>
<stroke
android:width="2dp"
android:color="@color/BLACK" />
【解决方案2】:
you might define an xml like below and use it where you need:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="7dp"
android:topRightRadius="7dp"
android:topLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp" />
<solid android:color="@android:color/white" />
<stroke android:width="1dip" android:color="#c9d1d7"/>
</shape>