如何使 Android EditText 文本后跟漂亮的蓝色下划线
How to make Android EditText text follow by a nice blue under line
我想要一个 EditText,如下图所示:-
请建议如何做到这一点?提前致谢!
更新:-
我采纳了 Nikita Kurtin 的建议。看起来很奇怪。我必须调整背景。我想这个背景需要针对不同的设备进行调整。看看图片现在是什么问题:-
您必须通过 java 代码创建您的编辑文本,我以前没有这样做过,但是 是一个例子,如果正在做同样的事情 thing.You 将需要对代码进行少量更改以满足您的实际需求。
希望这对你交配有所帮助。
我强烈建议在没有帮助文本、浮动标签文本和错误文本的布局的每一行中使用此库。
检查库:
https://github.com/rengwuxian/MaterialEditText
您可以为线条背景创建一个xml,并将它的tileMode 属性设置为repeated。然后将其用作编辑文本的背景。
xml 示例:
假设它被称为 'blue_lines.xml'
<?xml version="1.0" encoding="utf-8"?>
<bitmap
android:src="@drawable/blue_line_bg"
xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat"/>
editText 使用示例:
<EditText
android:id="@+id/input1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue_lines"
/>
Update1: 我用的蓝线图
Update2: 动态计算字体,适应不同屏幕
首先:获取blue_line_bg
的高度
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.blue_line_bg, options);
int imageHeight = options.outHeight;
int padding=10;//inner line padding
int fontSize=imageHeight-padding;//calculated font size
其次:将计算好的字体大小添加到相关的editText中
((EditText)findViewById(R.id.input1)).setTextSize(fontSize);//set calculated font size to the edit text
我想要一个 EditText,如下图所示:-
请建议如何做到这一点?提前致谢!
更新:-
我采纳了 Nikita Kurtin 的建议。看起来很奇怪。我必须调整背景。我想这个背景需要针对不同的设备进行调整。看看图片现在是什么问题:-
您必须通过 java 代码创建您的编辑文本,我以前没有这样做过,但是 是一个例子,如果正在做同样的事情 thing.You 将需要对代码进行少量更改以满足您的实际需求。
希望这对你交配有所帮助。
我强烈建议在没有帮助文本、浮动标签文本和错误文本的布局的每一行中使用此库。 检查库: https://github.com/rengwuxian/MaterialEditText
您可以为线条背景创建一个xml,并将它的tileMode 属性设置为repeated。然后将其用作编辑文本的背景。
xml 示例: 假设它被称为 'blue_lines.xml'
<?xml version="1.0" encoding="utf-8"?>
<bitmap
android:src="@drawable/blue_line_bg"
xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat"/>
editText 使用示例:
<EditText
android:id="@+id/input1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue_lines"
/>
Update1: 我用的蓝线图
Update2: 动态计算字体,适应不同屏幕
首先:获取blue_line_bg
的高度BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.blue_line_bg, options);
int imageHeight = options.outHeight;
int padding=10;//inner line padding
int fontSize=imageHeight-padding;//calculated font size
其次:将计算好的字体大小添加到相关的editText中
((EditText)findViewById(R.id.input1)).setTextSize(fontSize);//set calculated font size to the edit text