是否可以更改EditText
字段中显示的提示的字体?我想在 xml 本身中设置字体。
10 回答
您可以使用 SpannableString 和自定义 TypefaceSpan 对其进行更改。
首先,创建一个自定义 TypefaceSpan 类:
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface mNewType;
public CustomTypefaceSpan(Typeface type) {
super("");
mNewType = type;
}
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
mNewType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, mNewType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, mNewType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
}
然后只需将 TypefaceSpan 设置为 SpannableString:
TypefaceSpan typefaceSpan = new CustomTypefaceSpan(typeface);
SpannableString spannableString = new SpannableString(hintText);
spannableString.setSpan(typefaceSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
最后只需设置您的 EditText 的提示:
mEditText.setHint(spannableString);
我还没有找到任何有用的方法来更改 XML 中的提示字体。但是你可以这样实现:
mEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()== 0) {
//mEt.setTypeFace(normalFont);
}else{
// mEt.setTypeFace(hintFont);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
有一种非常简单的方法可以做到这一点。我刚刚在我的应用程序中做了,它奏效了。键也与 EditText 一起设置了 TextInputLayout 的 Facetype。
mEmailView.setTypeface(Typeface.createFromAsset(getAssets(), getString(R.string.app_font)));
((TextInputLayout) findViewById(R.id.tilEmail)).setTypeface(Typeface.createFromAsset(getAssets(), getString(R.string.app_font)));
@francisco_ssb 的回答是正确的。但是,我将提供一个替代解决方案,它不仅有助于更改提示的字体,还有助于更改其大小和样式。我希望这个解决方案会有所帮助。
1)创建自定义Hint
对象:
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.MetricAffectingSpan;
public class CustomHint extends SpannableString
{
public CustomHint(final CharSequence source, final int style)
{
this(null, source, style, null);
}
public CustomHint(final CharSequence source, final Float size)
{
this(null, source, size);
}
public CustomHint(final CharSequence source, final int style, final Float size)
{
this(null, source, style, size);
}
public CustomHint(final Typeface typeface, final CharSequence source, final int style)
{
this(typeface, source, style, null);
}
public CustomHint(final Typeface typeface, final CharSequence source, final Float size)
{
this(typeface, source, null, size);
}
public CustomHint(final Typeface typeface, final CharSequence source, final Integer style, final Float size)
{
super(source);
MetricAffectingSpan typefaceSpan = new CustomMetricAffectingSpan(typeface, style, size);
setSpan(typefaceSpan, 0, source.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
2)创建自定义MetricAffectingSpan
对象:
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
public class CustomMetricAffectingSpan extends MetricAffectingSpan
{
private final Typeface _typeface;
private final Float _newSize;
private final Integer _newStyle;
public CustomMetricAffectingSpan(Float size)
{
this(null, null, size);
}
public CustomMetricAffectingSpan(Float size, Integer style)
{
this(null, style, size);
}
public CustomMetricAffectingSpan(Typeface type, Integer style, Float size)
{
this._typeface = type;
this._newStyle = style;
this._newSize = size;
}
@Override
public void updateDrawState(TextPaint ds)
{
applyNewSize(ds);
}
@Override
public void updateMeasureState(TextPaint paint)
{
applyNewSize(paint);
}
private void applyNewSize(TextPaint paint)
{
if (this._newStyle != null)
paint.setTypeface(Typeface.create(this._typeface, this._newStyle));
else
paint.setTypeface(this._typeface);
if (this._newSize != null)
paint.setTextSize(this._newSize);
}
}
3)使用:
Typeface newTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf");
CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC, 60f);
// CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC);
// CustomHint customHint = new CustomHint(newTypeface, "Enter some text", 60f);
// CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC, 60f);
// CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC);
// CustomHint customHint = new CustomHint("Enter some text", 60f);
customEditText.setHint(customHint);
它在 XML 中是不可能的 -
文本和提示在 XML 中只能具有相同的字体。
我可以通过使用这个库来改变提示字体。
编译库后,您必须创建一个应用程序类和以下命令的类定义:
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("font.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
在您希望使用以下命令覆盖它的每个活动之后:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
采取使用文本输入布局并在其中放置文本输入编辑文本的方法
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout_Til">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editText"/>
</com.google.android.material.textfield.TextInputLayout>
在 Fragment/Activity 上,将提示所需的字体设置到 TextInputLayout(text_input_layout_Til)然后在编辑文本(editText)上设置字体/如果这样做,用户将输入的文本和提示将具有不同的字体
Android 8.0(API 级别 26)提供了一项新功能,可以更改 xml 中的字体以获取 EditText 中显示的提示。基于XML 字体的指南。
脚步:
1)创建一个新的资源目录:右键单击res文件夹,然后转到New -> Android resource directory。
2)将资源目录的名称设置为font。
3)在资源类型列表中,选择字体,然后单击确定。
4)在字体文件夹中添加您的自定义字体(例如 my_font.ttf) 。
5)在布局 XML 中,将fontFamily属性设置为字体文件:
<EditText
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/my_font"/>
...或者如果您想以编程方式执行此操作:
val typeface = resources.getFont(R.font.my_font)
edit_text.typeface = typeface
要在运行 Android 4.1(API 级别 16)及更高版本的设备上使用 XML 字体功能,请使用支持库 26:
注意:当您通过支持库在 XML 布局中声明字体系列时,请使用应用程序命名空间来确保您的字体加载。
1)在字体目录中创建custom_font.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/my_font"/>
</font-family>
2)设置fontFamily属性:
<EditText
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/custom_font"/>
...或者如果您想以编程方式执行此操作:
val typeface = ResourcesCompat.getFont(context, R.font.custom_font)
edit_text.typeface = typeface
在科特林:
nomeEditText.setText("New Text") //changes the text
nomeEditText.isEnabled = false //disables editable
nomeEditText.setBackgroundColor(Color.parseColor("#ffffff")) //changes background
nomeEditText.setTextColor(Color.parseColor("#737373")) //changes color text
nomeEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP,28f) //changes text size
nomeEditText.typeface = Typeface.create("@font/roboto", Typeface.BOLD) //changes font familly
nomeEditText.gravity = Gravity.CENTER_HORIZONTAL //centralize
使用此代码:
edittext.setAccentTypeface(typeface);