【问题标题】:Edittext background as straight lineEdittext背景为直线
【发布时间】:2016-01-04 09:57:36
【问题描述】:

我试图让edittext 背景变成一条直线,而不是通常的。我为此使用了自定义可绘制形状。但是该行被添加到edittext的中间而不是底部作为常规的edittext行。此外,当我按 Enter 键时,它不会像应有的那样保持在光标下方。有什么好办法吗?

自定义可绘制对象:

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

    <stroke 
        android:color="#FA58AC"
        android:width="1dp"/>

</shape>

【问题讨论】:

  • kishore 代码是正确的,但如果你想显示透明编辑文本的背景,请使用此

标签: android android-edittext


【解决方案1】:

使用这个

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape>
            <solid android:color="@android:color/white" />

            <stroke
                android:width="1dp"

                android:color="@android:color/holo_green_light" />
        </shape>
    </item>
</layer-list>

【讨论】:

    【解决方案2】:

    我认为这对你会有帮助。

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                // Your line color
                <solid android:color="@android:color/black" />
            </shape>
        </item>
        <item android:bottom="1dp">
            <shape android:shape="rectangle">
                // Your background color
                <solid android:color="@android:color/white" />
            </shape>
        </item>
    </layer-list>
    

    【讨论】:

      【解决方案3】:
               <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:text="Name"
                  android:textSize="10sp"
                  android:textColor="@color/color_black_light"
                  android:paddingLeft="10dp"
                  android:layout_marginTop="10dp" />
              <EditText
                  android:id="@+id/name"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="@null"
                  android:layout_marginTop="-10dp"
                  android:padding="10dp"
                  android:hint="Name"
                  android:inputType="text"
                  android:textColor="@color/black"
                  android:textColorHint="@color/color_black_light"
                  android:textCursorDrawable="@null" />
              <View
                  android:layout_width="match_parent"
                  android:layout_height="1dp"
                  android:background="@color/edittext_line_color" />
      

      【讨论】:

      • 请不要只是代码转储,添加一些关于您所做的工作以及如何解决 OPs 问题的背景信息
      猜你喜欢
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2012-04-11
      • 2011-08-08
      • 2017-03-23
      相关资源
      最近更新 更多