简体   繁体   English

为 ImageButton 的 drawable 添加阴影

[英]Adding shadow to drawable for a ImageButton

I have a image button which is designed like this我有一个像这样设计的图像按钮

 <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/plusOneButton"
            android:layout_marginBottom="5dp"
            android:src="@drawable/thinking"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:background="@drawable/button_border"
            />

The button_border xml looks like this. button_border xml 看起来像这样。

  <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <stroke
        android:width="30dp"
        android:color="#ff000000" />
    <padding
        android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp" />

</shape>

There fore the image is enclosed by a box, It doesn't look like a pressable button.因此图像被一个框包围,它看起来不像一个可按下的按钮。 How do i add shadows to it to make it look like a button?如何为其添加阴影以使其看起来像一个按钮? Kindly help, I am new to android.请帮忙,我是android新手。 I don't want to add ripple as the minimum api level in my app is 14.我不想添加涟漪,因为我的应用程序中的最低 api 级别是 14。

You can use the Layer-List which give you the ability to add two or more shapes in order to achieve the shadow then you need to create a selector drawable to set what are the drawable to use in each state like focus , pressed ... etc these links will help you check them :您可以使用图层列表,它使您能够添加两个或多个形状以实现阴影,然后您需要创建一个可绘制的选择器来设置在每个状态下使用的可绘制对象,例如 focus 、pressed ...等这些链接将帮助您检查它们:

layer-list层列表

http://nick.perfectedz.com/android-layerlist-tip/ http://nick.perfectedz.com/android-layerlist-tip/

Selector选择器

http://www.compiletimeerror.com/2014/03/android-button-selector-tutorial-with.html http://www.compiletimeerror.com/2014/03/android-button-selector-tutorial-with.html

i hope this will help you :)我希望这能帮到您 :)

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

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