简体   繁体   English

自定义按钮布局的阴影-Android

[英]Shadow for custom button layout-Android

This is my Button.xml file: 这是我的Button.xml文件:

<Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:id="@+id/tv4"

            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_below="@id/tv3"
            android:textStyle="bold"
            android:text="Contact"
            android:background="@drawable/custom_button"
            android:textSize="24sp"
            android:typeface="serif"
            android:layout_marginTop="2dip"
            android:layout_marginBottom="2dip"/>

This is my custom_button.xml 这是我的custom_button.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>        
    <shape>
        <gradient
            android:startColor="#48b7f4"
             android:endColor="#48b7f4"
            android:angle="270" />
        <!--stroke
            android:width="1dp"
            android:color="#000000" /-->
        <corners
            android:radius="6dp" />
    </shape>
</item></selector>

How do I add a shadow effect to my button? 如何为按钮添加阴影效果? I have tried shadowcolor option in xml but it is not working. 我已经尝试了xml中的shadowcolor选项,但无法正常工作。 I have 4 such buttons in the same layout. 我在相同的布局中有4个这样的按钮。 Thanks in advance 提前致谢

You could use layer-list resource to add multiple xml files like stack. 您可以使用图层列表资源来添加多个XML文件,例如堆栈。 shadow_button.xml: shadow_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:drawable="@drawable/shadow"/>
   <item
     android:drawable="@drawable/custom_button"
     android:bottom="4px" 
     android:right="4px"/>  
</layer-list>

The shadow.xml shadow.xml

<item>        
    <shape>
        <gradient
            android:startColor="#000000"
            android:endColor="#000000"
            android:angle="270" />
        <corners
            android:radius="6dp" />
    </shape>
 </item>
</selector>

You could provide colors in the gradient as you wish. 您可以根据需要在渐变中提供颜色。 Set shadow.xml as your buttons background. 将shadow.xml设置为按钮背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM