2017-08-06 33 views
0

我要添加這種類型的下劃線的EditText上如何EditText上的Android

example

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> 

但是當它添加上攻邊下劃線專注它顯示它自己的下劃線以及可以繪製的背景,並且您可以看到博士awable我使用的也不太好。

enter image description here

回答

1

只需創建像edit_text_design.xml XML文件,並將其保存到文件夾繪製

<?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> 

沒有他們需要..

輸出這樣 enter image description here

+0

你可以改變背景(主色)的顏色,強調... – 2017-08-06 06:05:18

+0

它的工作取代這些繪製但你能告訴我如何畫下劃線和文本之間的差距相同的樣本圖像以及如何改變它的狀態像正常和集中,因爲在目前的狀態,它只攜帶一種顏色在每個國家 – SogekingSenpai

+0

只需添加填充像... android :padding =「5dp」 – 2017-08-06 08:19:27

1

你必須從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"/> 
+0

感謝您的回覆非常感謝 – SogekingSenpai

0

添加此代碼來改變國家的行爲與納馬爾·哈克的答案沿

創建繪製,併爲其分配EDITTEXT

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

創建三個繪製並改變它們的顏色,只要你喜歡,並與