【问题标题】:How to add upside edged underline in edittext android如何在edittext android中添加向上边缘的下划线
【发布时间】:2017-08-06 05:15:34
【问题描述】:

我想在edittext中添加这种类型的下划线

xml

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/InputLayout">

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name" />

</android.support.design.widget.TextInputLayout>

风格

<style name="InputLayout" parent="TextAppearance.AppCompat">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">@color/grey</item>
    <item name="colorAccent">@color/white</item>
    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
    <item name="android:background">@drawable/edit_draw</item>
</style>

但是当它获得焦点时,它会显示它自己的下划线以及背景可绘制对象,并且您可以看到我使用的可绘制对象也无法正常工作。

【问题讨论】:

    标签: android android-layout android-edittext


    【解决方案1】:

    只需创建像edit_text_design.xml这样的xml文件并将其保存到您的drawable文件夹中

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <!-- underline color -->
    <item>
        <shape>
            <solid android:color="#41e3ec" />
        </shape>
    </item>
    
    <!-- main color -->
    <item
        android:bottom="1.5dp"
        android:left="1.5dp"
        android:right="1.5dp">
        <shape>
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    
    <!-- draw another block to cut-off the left and right bars -->
    <item android:bottom="5.0dp">
        <shape>
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    

    然后像这样在你的 xml 中为 EditText 使用它

    <android.support.design.widget.TextInputLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
    
       <EditText
            android:id="@+id/name_edit_text"
            android:background="@drawable/edit_text_design"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Name"/>
    </android.support.design.widget.TextInputLayout>
    

    不需要他们..

    这样的输出

    【讨论】:

    • 它有效,但你能告诉我如何在下划线和文本之间绘制一些与示例图像相同的间隙,以及如何改变它的状态,如正常和聚焦,因为在当前状态下,它每个都只带有一种颜色状态
    • 只需添加填充... android:padding="5dp"
    • 我自己搞定了 感谢您的支持我真的很感激它
    • android:padding="5dp" 表示所有边 5dp .... 你可以指定 android:paddingLeft , android:paddingRight, android:paddigTop, android:paddingBottom 你想...
    【解决方案2】:

    您必须从 EditText 内部布局中删除背景,因为它有自己的。

    <android.support.design.widget.TextInputEditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:hint="Name"/>
    

    【讨论】:

      【解决方案3】:

      添加此代码以更改状态行为以及 Enamul Haque 答案

      创建一个drawable并分配它Edittext

      编辑文本 xml

      <android.support.design.widget.TextInputLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/InputLayout">
      
              <android.support.v7.widget.AppCompatEditText
                  android:padding="7dp"
                  android:id="@+id/editText"
                  android:background="@drawable/editext_states"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:hint="Name" />
      
          </android.support.design.widget.TextInputLayout>
      

      可绘制

      <selector
      xmlns:android="http://schemas.android.com/apk/res/android"
      >
      <item
          android:state_pressed="true"
          android:drawable="@drawable/underline_blue"
          /> <!-- pressed -->
      <item
          android:state_focused="true"
          android:drawable="@drawable/underline_red"
          /> <!-- focused -->
      <item
          android:drawable="@drawable/underline_normal"
          /> <!-- default -->
      

      创建三个可绘制对象并根据需要更改它们的颜色并将这些可绘制对象替换为那个

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-28
        • 2017-03-17
        • 1970-01-01
        • 2012-05-08
        • 2013-03-31
        • 2017-11-20
        • 2013-11-06
        • 1970-01-01
        相关资源
        最近更新 更多