TextInputLayout浮动标签自定义字体和EditText上自定义字体
我有布局这样的 -TextInputLayout浮动标签自定义字体和EditText上自定义字体
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/form_username"/>
</android.support.design.widget.TextInputLayout>
根据我们的UI设计文件,我们需要有不同的自定义字体的浮动标签和EDITTEXT。
任何帮助表示赞赏。谢谢。
您可以通过这种方式做到这一点,
截至Design Library v23
,您可以使用TextInputLayout#setTypeface()
。
这将设置扩展和浮动提示的字体。
使用自定义跨度
final SpannableString ss = new SpannableString("Error");
ss.setSpan(new FontSpan(tf), 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
til.setError(ss);
private static final class FontSpan extends MetricAffectingSpan {
private final Typeface mNewFont;
private FontSpan(Typeface newFont) {
mNewFont = newFont;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setTypeface(mNewFont);
}
@Override
public void updateMeasureState(TextPaint paint) {
paint.setTypeface(mNewFont);
}
}
请不要只从其他用户的答案复制。如果您认为另一个帖子有解决方案,请将其投票为该帖子的副本。 –
我知道先生,但这个问题有些不同。 –
真的吗?即使您认为从答案中直接复制粘贴,解决方案是?通过“那里”,我的意思是[这篇文章](http://stackoverflow.com/questions/30765287/change-font-of-the-floating-label-edittext-and-textinputlayout),你刚才编辑的链接出于你的回答。 –
对于EditText上,您可以使用下面的类定义字体:
public class CustomEditText extends AppCompatEditText {
public CustomEditText (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public CustomEditText (Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomEditText (Context context) {
super(context);
init(null);
}
Typeface myTypeface;
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.CustomTextView);
String fontName = "Orkney Medium.otf";
if (fontName != null && !isInEditMode()) {
myTypeface = Typeface.createFromAsset(getContext().getAssets(),
fontName);
}
setTypeface(myTypeface);
a.recycle();
}
}
}
XML代码:
<com.utils.CustomEditText
android:id="@+id/edt_first_name_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="@string/hint_first_name"
android:inputType="textCapWords"
android:imeOptions="actionNext"
android:singleLine="true"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:paddingTop="@dimen/15dp"
android:textColor="@color/black"
android:textSize="20sp" />
对于TextInputLayout自定义类并不适用于所有情况,但你可以做这样的事情了优化的解决方案。
把这个在您的Java类:
public static void setTextInputLayoutTypeFace(Context mContext, TextInputLayout... textInputLayout) {
for (TextInputLayout til : textInputLayout) {
Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "Orkney Regular.otf");
til.setTypeface(typeface);
}
}
呼叫用上面的方法:
setTextInputLayoutTypeFace(mContext, tlFirstNameRegister, tlLastNameRegister,
tlUsernameRegister, tlEmailIdRegister, tlDateOfBirthRegister, tlMobileRegister, tlPasswordRegister,
tlConfPasswordRegister);
它不是最好的解决办法。但它会起作用。
你试过这个吗? http://stackoverflow.com/questions/3651086/android-using-custom-font – gonczor
基本上它会为TextView或EditText设置自定义字体。我想为浮动标签的TextInputLayout设置自定义字体。 –
可能的重复:http://stackoverflow.com/questions/30765287/change-font-of-the-floating-label-edittext-and-textinputlayout – rafsanahmad007