在Android Studio中围绕EditText的所有四个侧面投影 [英] Drop shadow around all the four sides of an EditText in Android Studio

查看:155
本文介绍了在Android Studio中围绕EditText的所有四个侧面投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,没有任何属性可以在布局文件中设置EditText字段周围的阴影.我搜索了问题,并找到了必须提供自定义背景xml drawable的解决方案.

As far as my knowledge aids me, there isn't any property to drop shadow around an EditText field, that can be set in the layout file. I searched for the problem and found solution that I've to provide a custom background xml drawable.

我的drawable/background_with_shadow看起来像这样:

My drawable/background_with_shadow looks like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-lis xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape
            android:shape="rectangle">
            <solid android:color="@android:color/darker_gray" />
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="2dp">
        <shape
            android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</layer-list>

并且我正在为包含EditText

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_with_shadow">
<EditText>
.
.
</EditText>
<LinearLayout>

但是,它只是在底角和底角周围投下了阴影. 我将如何在所有四个边角上做到这一点?

However it just drops the shadow around the bottom and bottom corners. How would I do it for all the four sides and corners?

我正在努力实现这一目标:

I'm trying to achieve this:

推荐答案

有一些更好的方法,可以在不使用layer-list的情况下使编辑文本周围出现阴影:

There are some better ways to get shadow around your edit text without using layer-list :

#1

EditText包裹在CardView中,如下所示:

Wrap your EditText in a CardView like this :

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="7dp"
    android:layout_margin="10dp"
    app:cardCornerRadius="0dp">

        <EditText
            android:id="@+id/msg_box"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:hint="Write a message"
            android:paddingLeft="5dp"
            android:paddingStart="5dp"
            android:textColorHint="#c7c7c7"
            android:textSize="15dp" />

</android.support.v7.widget.CardView>

输出:

使用9补丁作为EditText的背景:

Use a 9 patch as the background of your EditText :

    <EditText
        android:id="@+id/msg_box"
        android:padding="20dp"
        android:background="@drawable/shadow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:lines="5"
        android:gravity="top"
        android:hint="Enter your complaint..."
        android:textColorHint="#a7a7a7"
        android:textSize="15dp" />

输出

此处了解更多有关9patch的信息.

Read more about 9patch here.

这里是生成9个补丁阴影的强大在线工具.

Here's a great online tool to generate 9 patch shadow.

这篇关于在Android Studio中围绕EditText的所有四个侧面投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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