在安卓高版本,默認是有下劃線的,其默認下劃線的顏色是由其主題顏色來控制的!
控制如下:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> **<item name="colorAccent">@color/colorPrimaryDark</item>**
所以,只需要修改colorAccent的顏色,其下劃線的顏色既可以修改!
在低版本和高版本中,同樣是可以去添加下劃線的!方法有二:
方法一:
//此時必須要設置其背景為空 <EditText android:background="@null" android:drawableBottom="@drawable/line" android:hint="請輸入您的手機號碼" android:layout_width="match_parent" android:layout_height="wrap_content"/>
//資源名稱為 drawable/line <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/colorBlue" /> <size android:height="1dp" android:width="1000dp" /> </shape>
方法二:通過自定義editText
public class UnderLineEditText extends EditText { private Paint paint; public UnderLineEditText(Context context, AttributeSet attrs) { super(context, attrs); //設置畫筆的屬性 paint = new Paint(); paint.setStyle(Paint.Style.STROKE); //設置畫筆顏色為紅色 paint.setColor(Color.RED); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /**canvas畫直線,從左下角到右下角,this.getHeight()-2是獲得父edittext的高度,但是必須要-2這樣才能保證 * 畫的橫線在edittext上面,和原來的下劃線的重合 */ canvas.drawLine(0, this.getHeight()-2, this.getWidth()-2, this.getHeight()-2, paint); } }
這里有幾點需要注意:
其一:也可以繼承android.support.v7.widget.AppCompatEditText,但是有時會出現獲取不到焦點的現狀
其二:下劃線的的位置確定
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。