How to do an outer glow via xml on button?

自闭症网瘾萝莉.ら 提交于 2021-01-28 04:56:35

问题


I have a customized button with rounded corners, I put some shadow for when it's pressed but I want to do an outer shadow just to the bottom part of the button, I am making the drawable via xml, so if the glow could be that way would be great. These are the relevant parts of the code:

button_pressed_shadows.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
           <gradient 
               android:startColor="@color/black_overlay"
               android:endColor="@color/btn_login"
               android:angle="270"/>
           <corners android:radius="4dip" />
        </shape>
    </item>

    <item               
        android:top="2px">
        <shape android:shape="rectangle"> 
            <solid android:color="@color/btn_login"/>
            <corners android:radius="4dip" />
         </shape>
    </item>
</layer-list>

style_login.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed_shadows" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_login" /> <!-- default -->
</selector>

回答1:


Since I have the button design on photoshop, I made a 9 patch image with it, and put it on the style selector, everything went fine, think is the better (easiest) way.




回答2:


Also while using layer-list you can use appropriate color combinations and padding to make it like glow or shadow.

drawables using layer-list

edit_text_background

<?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="#fff" />

        <corners android:radius="4dp" />
    </shape>
</item>
<item android:bottom="1dp">
    <shape android:shape="rectangle" >
        <corners android:radius="4dp" />

        <stroke
            android:width="1dp"
            android:color="#dadad7" />

        <solid android:color="#fff" />
    </shape>
</item>



来源:https://stackoverflow.com/questions/21683706/how-to-do-an-outer-glow-via-xml-on-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!