一、布局文件activity_custom_hint.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".CustomHintActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:gravity="center|end" android:text="用户名称:" android:textColor="@color/black" android:textSize="15sp" /> <EditText android:id="@+id/editText" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="3" android:textSize="13sp" /> </LinearLayout> </LinearLayout>
二、CustomHintActivity类,在java代码中设置提示文本内容和字体大小
public class CustomHintActivity extends AppCompatActivity { private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_hint); editText = findViewById(R.id.editText); SpannableString spannableString = new SpannableString("请输入用户名称"); //设置文字绝对大小 //第一个参数:size默认单位为px //第二个参数:当为true时,单位为dip; 为false时,单位为px AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(13, true); spannableString.setSpan(absoluteSizeSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置提示文本 editText.setHint(spannableString); } }
效果如图所示: