Android 向 EditText 字段添加投影效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10850726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:13:29  来源:igfitidea点击:

Add drop shadow effects to EditText Field

androidxmlandroid-edittexteffectsdropshadow

提问by swiftBoy

I am trying to design an EditText Fieldhaving Shadows (bottom and right side) like this

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

enter image description here

在此处输入图片说明

tried googling & hunted many SO discussions but all are for TextView not EditText.

尝试使用谷歌搜索并搜索了许多 SO 讨论,但都是针对 TextView 而不是 EditText。

This is my code adding shadow to Input Text but not to TextField

这是我的代码添加阴影到输入文本而不是文本字段

<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>


I guess it needs some custom xml view in drawable but not getting exact idea. What will be the logic to achieve this.

我想它需要一些自定义的 xml 视图 drawable 但没有得到确切的想法。实现这一目标的逻辑是什么。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by swiftBoy

Well.. @Shalini's answer helped me in this way but still I got another way to achieve 2D shadow with EditText Field and I am going to share with you.

好吧..@Shalini 的回答以这种方式帮助了我,但我仍然有另一种方法来使用 EditText Field 实现 2D 阴影,我将与您分享。

We need to create custom XML view with three layer for EditText, bottom shadow and right side shadow

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

Below is my code.

下面是我的代码。

res/drawable/edittext_shadow.xml

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>


Now we can set this shadow view to our TextField using "Background" property

现在我们可以使用“Background”属性将此阴影视图设置为我们的 TextField

like this

像这样

res/layout/main.xml

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>


and the result screen is like I have posted in question above.

结果屏幕就像我在上面发布的问题一样。

Thanks to SO, sharing knowledge.

感谢 SO,分享知识。

回答by Shalini

This works for me..

这对我有用..

   <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" />

Hope it helps:)

希望能帮助到你:)

回答by Shrikant

From Shadow Effect for a Text in Android?, perhaps you'd consider using

阴影效果在Android的文本?,也许你会考虑使用

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

Alternatively:

或者:

setShadowLayer()