更改EditText底部边框的大小

19

我在代码中有一些 EditText ,我想让它的底部边框变细一点。在互联网上找不到任何关于这方面的信息,也许这里有人能帮我。

我现在有的效果: enter image description here

我想要的效果: enter image description here


下划线或底部边框?因为下划线是字体属性而不是 GUI。 - anshuVersatile
@anshu 我想我意思是底部边框。 - Ron Makila
2个回答

47

尝试使用以下方法来实现焦点效果:

edt_bg_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/edt_bg_selected" android:state_focused="true"/>
<item android:drawable="@drawable/edt_bg_normal" android:state_focused="false"/>
</selector>

edt_bg_normal.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="1px"
            android:color="#FF000000" />

        <solid android:color="#00FFFFFF" />

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

edt_bg_selected.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="1px"
            android:color="#ff0000" />

        <solid android:color="#00FFFFFF" />

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

并将您的编辑文本更改为:

 <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edt_bg_selector" />

还有一个问题,我需要更改哪个属性才能使边框变得稍微厚一点?@Srikanth - Ron Makila
我认为我们无法绘制比1像素更细的可绘制对象,因为它是基本单位。你可以设置0.01dp这样的值,但是使用公式'px = (int)(scale * dp + 0.5)'时,它仍将四舍五入为1像素。其中scale为'mdpi为1','hdpi为1.5','xhdpi为2'和'xxhdpi为3'。 - Srikanth
1
不,我想让它更厚一点而不是更薄...将 android:width 更改为 2px3px 并没有改变任何东西 @Srikanth - Ron Makila
当我在Android Studio中将此代码添加到我的项目中时,在设计编辑器中,线条变为白色(如我所定义的),但是当我在手机上运行它时,线条又变成了黑色(默认颜色)。为什么? - MoOoG
@Srikanth 谢谢 :) - Subhan Ali
显示剩余2条评论

3
尝试以下操作:
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:hint="Biografie"
        android:textColorHint="#99000000"
        android:drawableLeft="@drawable/quote_img" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#80000000" />
</LinearLayout>

或者

将此 btm_line_shape.xml 文件保存在您的 drawable 文件夹中:

<?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="1px"
            android:color="#FF000000" />

        <solid android:color="#00FFFFFF" />

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

并且

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Biografie"
    android:textColorHint="#99000000"
    android:background="@drawable/btm_line_shape"
    android:drawableLeft="@drawable/quote_image" />

谢谢,完美地解决了!你知道我怎么能改变EditText被点击时的焦点颜色吗?@Srikanth - Ron Makila
请查看:https://dev59.com/8mAg5IYBdhLWcg3wlbxh - Srikanth
请将上述 btm_line_shape.xml 更改为此链接中的样式,并将其应用为背景。 - Srikanth
现在使用链接的代码时会发生这种情况:当我点击EditText时,会快速显示EditText周围的矩形,但我只想在焦点处保持红色边框。@Srikanth - Ron Makila
我发布了另一个答案,请查看。 - Srikanth

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