Android 如何仅在 EditText 底部添加边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25481244/
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 09:28:33  来源:igfitidea点击:

how add a border only on bottom of EditText?

android

提问by Manjeet Brar

I saw many related discussions but didn't find a suitable solution. What I need to do is to create a black border on the bottom side of an EditText widget. Is that possible?

我看了很多相关的讨论,但没有找到合适的解决方案。我需要做的是在 EditText 小部件的底部创建一个黑色边框。那可能吗?

采纳答案by KOTIOS

I compiled this at my end :

我最后编译了这个:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"><layer-list>
                <item android:bottom="-10dp" android:left="-10dp" android:right="-10dp"><shape>
                        <gradient android:angle="270" android:endColor="#a0e0b071" android:startColor="#a0a67637" />

                        <stroke android:width="10dp" android:color="#5c3708" />

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

                        <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
                    </shape></item>
            </layer-list></item>
        <item android:state_enabled="true"><layer-list>
                <item android:bottom="-10dp" android:left="-10dp" android:right="-10dp"><shape android:shape="rectangle">
                        <gradient android:angle="270" android:endColor="#a0a67637" android:startColor="#a0e0b071" />

                        <stroke android:width="10dp" android:color="#5c3708" />

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

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

    </selector>

回答by Dhruv Raval

simple create one edittext_bottom_line.xmlfile in drawablefolder in res

简单 在res 的drawable文件夹中创建一个edittext_bottom_line.xml文件

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

    <item android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="0.5dp"
                android:color="@android:color/black" />
        </shape>
    </item>

</layer-list>

To set control's single property like these:

像这样设置控件的单个属性:

 <EditText
     android:id="@+id/editTextEnterPassword"
     android:layout_width="fill_parent"
     android:layout_height="40dp"
    android:background="@drawable/edittext_bottom_line"
    /> 

回答by Subhalaxmi

just add underlinebg.xmlin res/drawable

只需在res/drawable 中添加 underlinebg.xml

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

    <item android:bottom="-1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="2dp"
                android:color="@color/drawe_content_color_click" />
        </shape>
    </item>

</layer-list> 

and set the background where ever required. For me i got the solution like [![myoutput!!][1]][1] .I think my answer will helpful for others.

并在需要的地方设置背景。对我来说,我得到了类似 [![myoutput!!][1]][1] 的解决方案。我认为我的回答对其他人有帮助。

回答by Anmol Juneja

Create a layer list with item as below

创建一个包含项目的图层列表,如下所示

<item android:bottom="-1dp"
    android:left="-2dp"
    android:right="-2dp"
    android:top="-2dp">
    <shape android:shape="rectangle" >
        <stroke
            android:width="2dp"
            android:color="#111111" />
    </shape>
</item>

回答by Shayan Pourvatan

you can create one xml file like follow:

您可以创建一个 xml 文件,如下所示:

<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/orange" />
    </shape>
</item>
<item android:bottom="4dp">
    <shape android:shape="rectangle" >
        <size android:height="4dp" />

        <solid android:color="@color/green3" />
    </shape>
</item>

then set this file as your background

然后将此文件设置为您的背景

回答by The EasyLearn Academy

<?xml version="1.0" encoding="utf-8" `enter code here`?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="2dp" android:right="-2dp"
        android:top="-2dp" android:bottom="2dp" >
        <shape android:shape="rectangle">
            <stroke android:color="@android:color/black" android:width="2dp">

            </stroke>
        </shape>
    </item>
</layer-list>