在 Android Studio 中在 EditText 的所有四个边上放置阴影 [英] Drop shadow around all the four sides of an EditText in Android Studio

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

问题描述

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

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

我正在为我的 LinearLayout 设置此属性,其中包含一个 EditText

但是,它只会在底角和底角周围投下阴影.我将如何处理所有四个边和角?

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

解决方案

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

#1

将您的 EditText 包装在 CardView 中,如下所示:

2

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

 <EditTextandroid:id=@+id/msg_box"安卓:填充=20dp";android:background=@dr​​awable/shadow"android:layout_width=match_parent"android:layout_height="wrap_content";android:layout_centerVertical=真"安卓:线=5";安卓:重力=顶部";android:hint="输入您的投诉..."android:textColorHint="#a7a7a7";android:textSize="15dp";/>

输出

阅读更多关于 9patch 这里.

这里是一个很棒的在线工具,可以生成 9 个补丁阴影.

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.

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>

and I'm setting this property for my LinearLayout which contains an 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:

解决方案

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

#1

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>

OUTPUT :

2

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

OUTPUT

Read more about 9patch here.

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

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

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