Android定制自己的EditText輕松改變底線顏色
最近做 android 項(xiàng)目遇到這個(gè)問題,為了保持 app 風(fēng)格一致,需要將原生的EditText底線顏色改成橙色。網(wǎng)上搜了一些解決方案,特此記錄總結(jié)一下。
效果圖
默認(rèn)的EditText底線顏色是藍(lán)色的,
我們想實(shí)現(xiàn)橙色的效果
實(shí)現(xiàn)方法
1、準(zhǔn)備兩個(gè)背景圖
一個(gè)作為 edittext 的默認(rèn)背景 , 另一個(gè)作為 輸入時(shí)候的背景
Note
使用 9.png, 不要用png, 否則圖片會(huì)模糊, 花掉
在文件夾 drawable 用selector 建立一個(gè)xml 文件
<!-- drawable/edittext_shape.xml --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/edittext_default" android:state_focused="false"/> <item android:drawable="@drawable/edittext_focused" android:state_focused="true"/> </selector>
在 values 文件夾 下面的 styles.xml 新建一個(gè)style
此步驟是為了復(fù)用這個(gè)樣式, 也可以不用style, 直接在 layout里的布局 xml 里 寫代碼
<!-- drawable/values/styles.xml --> <style name="SmsEditText"> <item name="android:layout_marginLeft">20dp</item> <item name="android:layout_marginRight">20dp</item> <item name="android:layout_marginTop">20dp</item> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textSize">16sp</item> <item name="android:background">@drawable/edittext_shape</item> </style>
在layout的布局文件中引用定制的edittext
<!-- drawable/layout/fragment_bomb.xml --> <LinearLayout android:id="@+id/input" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp" > <EditText android:id="@+id/phone" style="@style/SmsEditText" android:hint="@string/phone_hint" android:inputType="phone" android:maxLength="11" android:maxLines="1" /> <EditText android:id="@+id/times" style="@style/SmsEditText" android:hint="@string/times_hint" android:inputType="number" android:maxLines="1" /> </LinearLayout>
在edittext 底部加上一條直線( 仿微信)
原生的效果是edittext底部是一個(gè)凹形的線,這樣不是很美觀。微信的輸入框下面是一條直線。如何實(shí)現(xiàn)呢?可以將上面的圖片改成直線型的,不過需要美工人員 PS 的幫忙。我們也可以利用 xml 文件來畫出圖形,完成所需的直線效果。
利用xml 畫線
本來想利用xml 畫線, 做出微信 輸入框 的那種下面是一條直線,發(fā)現(xiàn)純粹用xml不美觀, 這種還是讓美工做一個(gè)背景圖可能比較好。
查看這篇文章:android利用xml實(shí)現(xiàn)分割線
edittext 去除邊框
android:background="@null"
這個(gè)代碼可以去掉 edittext 的邊框
edittext 底部加線
在drawable 新建一個(gè) line.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@color/orange_normal" /> <size android:height="1dp" android:width="1000dp" /> </shape>
在layout 的布局文件中引用
<EditText android:id="@+id/phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:drawableBottom="@drawable/line" android:hint="@string/phone_hint" android:inputType="phone" android:maxLength="11" android:maxLines="1" />
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android取消EditText自動(dòng)獲取焦點(diǎn)默認(rèn)行為
- Android控件系列之EditText使用方法
- android同時(shí)控制EditText輸入字符個(gè)數(shù)和禁止特殊字符輸入的方法
- Android中EditText實(shí)現(xiàn)不可編輯解決辦法
- Android編程設(shè)置TextView顏色setTextColor用法實(shí)例
- Android更改EditText下劃線顏色樣式的方法
- Android 設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤
- 全面解析Android中對(duì)EditText輸入實(shí)現(xiàn)監(jiān)聽的方法
- android基礎(chǔ)教程之a(chǎn)ndroid的listview與edittext沖突解決方法
- Android中EditText setText方法的踩坑實(shí)戰(zhàn)
相關(guān)文章
Android之ScrollView嵌套ListView和GridView沖突的解決方法
由于ListView,GridView本身都繼承于ScrollView,一旦在ScrollView中嵌套ScrollView,在ScrollView中嵌套使用ListView或者GridView,ListView只會(huì)顯示一行多一點(diǎn)。兩者進(jìn)行嵌套,即會(huì)發(fā)生沖突2013-09-09Android注冊(cè)登錄實(shí)時(shí)自動(dòng)獲取短信驗(yàn)證碼
注冊(cè)登錄或修改密碼功能常常需要輸入短信驗(yàn)證碼,如何自動(dòng)獲取短信驗(yàn)證碼,這篇文章就為大家介紹了Androidcv注冊(cè)登錄自動(dòng)獲取短信驗(yàn)證碼的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-08-08Android App支付系列(二):支付寶SDK接入詳細(xì)指南(附官方支付demo)
本篇文章介紹了Android App支付系列(二):支付寶SDK接入詳細(xì)指南(附官方支付demo) ,有興趣的同學(xué)可以了解一下。2016-11-11[Android] 通過GridView仿微信動(dòng)態(tài)添加本地圖片示例代碼
本篇文章主要介紹了[Android] 通過GridView仿微信動(dòng)態(tài)添加本地圖片示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01Android ListView用EditText實(shí)現(xiàn)搜索功能效果
本篇文章主要介紹了Android ListView用EditText實(shí)現(xiàn)搜索功能效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android仿新浪微博oauth2.0授權(quán)界面實(shí)現(xiàn)代碼(2)
這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博oauth2.0授權(quán)界面實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11React?Native之在Android上添加陰影的實(shí)現(xiàn)
這篇文章主要介紹了React?Native之在Android上添加陰影的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03