安卓按键的背景形状与阴影 [英] Android Button's background as shape with Shadow

查看:258
本文介绍了安卓按键的背景形状与阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个按钮的背景,从形状和正在找我的目的相当不错。唯一需要的是放弃了一点阴影吧。 这里是code:

I've made a button background from shapes and is looking quite good for my purpose. The only thing needed is to drop a bit of shadow for it. Here is the code:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#343434" />

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

            <corners android:radius="3dp" />

            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient android:angle="270" android:endColor="#E2E2E2" android:startColor="#BABABA" />

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

            <corners android:radius="4dp" />

            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
   </item>

</selector>

下面是我想达到

我如何删除影子?我的猜测是,我需要另一种形状,但有黑色/灰色的背景和设定某种TOPA第二缘左侧填充,使它看起来像一个影子。但我不知道该怎么办呢?和文件并没有帮助我太多。

How do I drop the shadow ? My guess is that I need to make another shape but with black/gray background and set some sort of topa nd left padding of margin to make it look like a shadow. But I don't know how to do it... and documentation didn't helped me too much.

后来编辑:我想通过code添加阴影的XML文件,而不是。 谢谢你。

Later I want to add the shadow in xml file and not by code. Thanks.

推荐答案

如果你想堆更多的形状,一个在彼此的顶部,那么你可以使用<一个href="http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList"><$c$c>layer-list.贝娄是code在您选择正常的项目(与灰色的条):

If you want to stack more shapes one on top of each other then you could use a layer-list. Bellow is the code for the normal item in your selector(with a strip of gray color):

<item><layer-list>
            <item android:right="5dp" android:top="5dp"><shape>
                    <corners android:radius="3dp" />

                    <solid android:color="#D6D6D6" />
                </shape></item>
            <item android:bottom="2dp" android:left="2dp"><shape>
                    <gradient android:angle="270" android:endColor="#E2E2E2" android:startColor="#BABABA" />

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

                    <corners android:radius="4dp" />

                    <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
                </shape></item>
        </layer-list></item>

现在的问题是,你将无法实现真正​​的影子看看你的按钮这种类型的可绘制的。你可以使用code从已影子就可以了对方的回答或九片图像。

The problem is that you'll not be able to achieve a true shadow look on your Button with this type of drawable. You could use the code from the other answer or a nine patch image that already has shadow on it.

这篇关于安卓按键的背景形状与阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆