为EditText字段添加投影效果

16
我正在尝试设计一个类似这样有阴影(底部和右侧)的EditText字段

enter image description here

我试过搜索和查找许多SO讨论,但所有讨论都是针对TextView而不是EditText。
这是我的代码将阴影添加到输入文本但未添加到文本字段。
<EditText android:id="@+id/txtpin" 
        android:maxLength="4" 
        android:layout_marginLeft="10dp" 
        android:layout_height="37dp" 
        android:gravity="center_horizontal" 
        android:inputType="textPassword" 
        android:longClickable="false" 
        android:layout_width="160dp" 

        android:shadowColor="@color/Black"
        android:shadowDx="1.2"
        android:shadowDy="1.2"
        android:shadowRadius="1.5" 

        android:background="@color/White">
            <requestFocus></requestFocus>
        </EditText>
我猜需要在drawable中创建一些自定义的xml视图,但是没有确切的想法。实现这个的逻辑是什么?任何帮助都将不胜感激。

你可以为 EditText 设置背景图片......一张在你问题中显示的图片。 - MAC
@gtumca-MAC.. 谢谢,但我只想用编程来解决问题,顺便说一句,问题已经解决了! - swiftBoy
@RDC,我试过你的解决方案了,但我的情况是效果应该朝内,有什么解决办法吗?http://stackoverflow.com/questions/19901676/effect-in-edittext-from-xml#comment29616414_19901676 - surhidamatya
@sur007 我想你应该寻找"内阴影",请参考这个解决方案,以及这个链接或许会对你有所帮助。 - swiftBoy
3个回答

29

好的..@Shalini的答案帮助了我,但我找到了另一种方法来实现带有EditText字段的2D阴影效果,我将与你分享。

我们需要创建自定义的XML视图,为EditText、底部阴影和右侧阴影创建三个层

下面是我的代码。

res/drawable/edittext_shadow.xml

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

<!-- most important is order of layers -->

    <!-- Bottom right side 2dp Shadow -->
    <item >
        <shape android:shape="rectangle">
            <solid android:color="#000000" />           
        </shape>
    </item>

    <!-- Bottom 2dp Shadow -->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#000000" />   
        </shape>
    </item>

    <!-- White Top color -->
    <item android:bottom="3px" android:right="3px">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />           
        </shape>
    </item> 
</layer-list>

现在我们可以使用“Background”属性将这个阴影视图设置为我们的文本字段。

就像这样:

res/layout/main.xml

<EditText android:layout_width="wrap_content" 
            android:id="@+id/txtpin"  
            android:maxLength="4" 
            android:layout_height="37dp" 
            android:gravity="center_horizontal" 
            android:longClickable="false" 
            android:padding="2dp"

            android:inputType="textPassword|number" 
            android:password="true" 
            android:background="@drawable/edittext_shadow" 
            android:layout_weight="0.98" 
            android:layout_marginLeft="15dp">
                <requestFocus></requestFocus>
   </EditText>

结果屏幕就像我在上面发布的问题中所述。

感谢 StackOverflow 分享知识。


5
答案在功能上是正确的,但是在layer-list中没有必要使用第二个项目。第一个项目足以实现两个阴影效果。 - SohailAziz

6
这对我很有效。
   <EditText 
       android:layout_width="fill_parent" 
       android:shadowRadius="2"  
       android:shadowColor="#0000ff"
       android:shadowDx="2"
       android:shadowDy="4" 
       android:id="@+id/EditText01" 
       android:layout_height="wrap_content" />

希望这能有所帮助 :)

20
你的解决方案对我不起作用。它给文本(EditText内容)添加了阴影,而不是边框的EditText组件。我有遗漏什么吗? - CyberMJ

2

Android 文字阴影效果?中,也许您可以考虑使用

android:shadowColor, 
android:shadowDx,
android:shadowDy,
android:shadowRadius;

或者:

setShadowLayer()

这个线程我已经检查过了(请看我的代码),它是针对TextView的。无论如何,谢谢! - swiftBoy
这并没有提供问题的答案。如果您想对作者进行批评或请求澄清,请在他们的帖子下留言。 - Gabriele Mariotti
3
嗨 @Gabriele!老实说,三年前我回答这个问题是希望能赚取声誉。我会编辑这个答案。谢谢! - Shrikant Ballal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接