Android TextInputLayout浮动标签自定义字体和edittext自定义字体

Android TextInputLayout浮动标签自定义字体和edittext自定义字体,android,xml,android-layout,android-textinputlayout,Android,Xml,Android Layout,Android Textinputlayout,我有这样的布局- <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:layout_width="mat

我有这样的布局-

<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设计文档,我们需要为浮动标签和编辑文本使用不同的自定义字体


感谢您的帮助。谢谢。

你可以用这种方式来做

从设计库v23开始,您可以使用

这将在扩展提示和浮动提示上设置字体

使用自定义范围

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);
    }
}



对于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" />
setTextInputLayoutTypeFace(mContext, tlFirstNameRegister, tlLastNameRegister,
            tlUsernameRegister, tlEmailIdRegister, tlDateOfBirthRegister, tlMobileRegister, tlPasswordRegister,
            tlConfPasswordRegister);
使用调用上述方法:

  <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" />
setTextInputLayoutTypeFace(mContext, tlFirstNameRegister, tlLastNameRegister,
            tlUsernameRegister, tlEmailIdRegister, tlDateOfBirthRegister, tlMobileRegister, tlPasswordRegister,
            tlConfPasswordRegister);

这不是最好的解决办法。但它会起作用。

你试过这个吗?基本上,它将为TextView或EditText设置自定义字体。我想为TextInputLayout的浮动标签设置自定义字体。可能的重复:请不要仅复制其他用户的答案。如果你认为另一个帖子有解决办法,就把它当作该帖子的翻版。我知道,先生,但这个问题有什么不同。真的吗?即使你认为从答案直接复制粘贴有解决方案吗?我所说的“there”,是指你刚刚从答案中删掉的链接。我知道我已经编辑了,但已经向提问者提供了解决方案,并提供了组合解决方案。我不知道OP在哪里询问错误文本。你读过你剽窃的代码吗?现在这是剽窃,因为你已经删除了任何类型的归属。