【问题标题】:Android drawable with background and gradient on the leftAndroid drawable,左侧有背景和渐变
【发布时间】:2012-11-27 19:45:28
【问题描述】:

W 想要一个drawable,它的左侧有背景和渐变,大约是10dp 宽。

我想要实现的图像:

  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>

【问题讨论】:

  • 您能否更准确地解释您需要什么,如果您有更好的期望图像上传,将提供更好的想象力。
  • 我已经上传了图片,它会更清楚地解释它(我希望;))

标签: android drawable gradient shape


【解决方案1】:

在可绘制文件夹中创建 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 并通过以下方式引用它:

android:background="@drawable/sidecolor"

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>

输出图像:

希望能帮到你。

【讨论】:

  • 我想向左对齐 - 在您的示例中,您已将其硬编码为 280dp
  • @pixel 据我所知这是我的做法
  • 但是如果我想让这个黑盒子半透明怎么办?我最终会在下面有渐变......
  • 完全正确 - 我希望它是半透明的。
猜你喜欢
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 2011-08-24
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多