W 想要一个 drawable
,它在左侧有背景和渐变,宽度约为 10dp
。
我想要实现的目标的图片:
- 左侧红色渐变
- 其他背景
我怎样才能做到这一点?
我尝试过使用两种形状的 layer-list
但没有成功。
项目背景:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/background" />
<item android:drawable="@drawable/gradient" />
</layer-list>
可绘制的背景:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/black" />
</shape>
可绘制的形状:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="90"/>
<size android:width="10dp" />
</shape>
最佳答案
在 drawable 文件夹中创建 sidecolor(或任何你想要的名称)XML:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background" android:bottom="5dp"
android:top="5dp" android:left="5dp" android:right="5dp"/>
<item android:drawable="@drawable/red" android:bottom="5dp" android:top="5dp"
android:left="5dp" android:right="280dp" />
</layer-list>
然后创建背景 XML:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/black" />
</shape>
然后红色 XML 作为形状:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#B22222" />
</shape>
输出图像:
您还可以创建红色 XML 作为渐变:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#B22222" android:centerColor="#FFFFFF"
android:endColor="#B22222" android:angle="0" />
</shape>
输出图像:
更新:
您也可以通过这种方式将其左对齐,还可以根据需要控制其大小,
首先创建一个 XML 并将其命名为 side color.xml 并通过以下方式引用 View :
android:background="@drawable/sidecolor"
边色.xml :
<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:left="10dp">
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
输出图像:
希望对你有帮助。
关于带有背景和左侧渐变的 Android 可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13591836/