带有背景和左侧渐变的 Android 可绘制对象

标签 android drawable gradient shapes

W 想要一个 drawable,它在左侧有背景和渐变,宽度约为 10dp

我想要实现的目标的图片:

enter image description here

  1. 左侧红色渐变
  2. 其他背景

我怎样才能做到这一点?

我尝试过使用两种形状的 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> 

输出图像:

enter image description here

您还可以创建红色 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>

输出图像:

enter image description here

更新:

您也可以通过这种方式将其左对齐,还可以根据需要控制其大小,

首先创建一个 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>

输出图像:

enter image description here

希望对你有帮助。

关于带有背景和左侧渐变的 Android 可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13591836/

相关文章:

java - 当我使用两个图时,如何在导航图中获取当前选定的 fragment ID

android - 形状为背景,边框很小

android - 无论 drawableLeft 如何将文本居中放置在 Button 中?

php - 动态多色渐变图像

android - QuickBlox - 来自不同位置/设备的相同用户登录

android - 从 Google 云端硬盘打开自定义文件失败

android - 如何在 Sqlite android 中用算术表达式更新一行

android - 如何使用 LayerDrawable 解决 OutOfMemoryError

java - 自定义渐变按钮 - 看不到文本

html - 如何从右到左淡出图像?