更改浮动标签EditText和TextInputLayout的字体

use*_*939 25 android android-edittext android-design-library android-textinputlayout material-components

有人试图改变浮标的字体吗?我更改了EditText的源代码,但浮动标签的字体没有改变,我非常感谢那些帮助我的人

码:

               <android.support.design.widget.TextInputLayout
                    android:id="@+id/tilTextoDescricao"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/tilValorUnidade"
                    android:layout_marginTop="10dp">

                    <EditText
                        android:id="@+id/etTextoDescricao"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:hint="Descrição"
                        android:textSize="15dp"
                        android:inputType="text" />

                </android.support.design.widget.TextInputLayout>

----------------- 

   etTextoDescricao= (EditText) findViewById(R.id.etTextoDescricao);
  etTextoDescricao.setTypeface(CustomTypeface.getTypefaceMediumDefault(this));
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Aus*_*ney 29

截至Design Library v23,您可以使用TextInputLayout#setTypeface().

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

以下是b.android.com上讨论的功能请求.

编辑:该错误观点字样没有被设置,但现在固定v25.1.0.

  • 我做了这个,除了错误视图之外,它适用于所有内容.我认为textappearance会覆盖它,所以这可能是某种错误.要以特定方式定位错误视图,您可以使用android-given ID来引用它,如下所示:**((TextView)inputLayout.findViewById(R.id.textinput_error)).setTypeface(customFont);** (4认同)

adn*_*eal 19

不幸的是,你必须使用反射来处理这个问题.

浮动标签是绘制的CollapsingTextHelper,它是一个内部的包私有类,并不设置为处理跨度.因此,使用类似自定义的东西TypefaceSpan在这种情况下不起作用.

因为它使用反射,所以不保证将来可以使用.

履行

final Typeface tf = Typeface.createFromAsset(getAssets(), "your_custom_font.ttf");
final TextInputLayout til = (TextInputLayout) findViewById(R.id.yourTextInputLayout);
til.getEditText().setTypeface(tf);
try {
    // Retrieve the CollapsingTextHelper Field
    final Field cthf = til.getClass().getDeclaredField("mCollapsingTextHelper");
    cthf.setAccessible(true);

    // Retrieve an instance of CollapsingTextHelper and its TextPaint
    final Object cth = cthf.get(til);
    final Field tpf = cth.getClass().getDeclaredField("mTextPaint");
    tpf.setAccessible(true);

    // Apply your Typeface to the CollapsingTextHelper TextPaint
    ((TextPaint) tpf.get(cth)).setTypeface(tf);
} catch (Exception ignored) {
    // Nothing to do
}
Run Code Online (Sandbox Code Playgroud)

错误视图

如果您需要更改错误的字体,可以执行以下两项操作之一:

  1. 使用Reflection抓取错误TextViewTypeface像以前一样应用
  2. 使用自定义范围.与浮动标签不同,使用的错误视图TextInputLayout只是一个TextView,因此它能够处理跨度.

用反射

final Field errorField = til.getClass().getDeclaredField("mErrorView");
errorField.setAccessible(true);
((TextView) errorField.get(til)).setTypeface(tf);
Run Code Online (Sandbox Code Playgroud)

使用自定义范围

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

}
Run Code Online (Sandbox Code Playgroud)

结果

结果

我使用的字体是Smoothie Shoppe.

  • 看来你现在可以在TextInputLayout上使用`setTypeface(Typeface typeface)`.:) (7认同)
  • 不漂亮,但现在非常有用,谢谢! (3认同)
  • 我只想更改浮动标签字体。是否可以? (2认同)

azi*_*ian 11

我正在使用新MaterialComponents主题,但没有一个答案对我有帮助.

不得不自己玩风格和主题.将发布一大块样式,以防有人面临同样的问题.

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
  ...
  <item name="textInputStyle">@style/CustomFontTextInputLayout</item>
</style>  

<!-- region TextInputLayout & TextInputEditText styles -->
<style name="TextInputLayout.OutlineBox.CustomFont" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
  <item name="android:theme">@style/ThemeOverlay.TextInputEditText.OutlinedBox.CustomFont</item>
</style>

<style name="ThemeOverlay.TextInputEditText.OutlinedBox.CustomFont" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
  <item name="editTextStyle">@style/TextInputEditText.OutlinedBox.CustomFont</item>
</style>

<style name="TextInputEditText.OutlinedBox.CustomFont" parent="Widget.MaterialComponents.TextInputEditText.OutlinedBox">
  <item name="android:fontFamily">@font/my_font</item>
</style>

<style name="CustomFontTextInputLayout" parent="Widget.Design.TextInputLayout">
  <item name="hintTextAppearance">@style/TextInputLayoutHintText</item>
  <item name="helperTextTextAppearance">@style/TextInputLayoutHelperText</item>
  <item name="errorTextAppearance">@style/TextInputLayoutErrorText</item>
</style>

<style name="TextInputLayoutHintText" parent="TextAppearance.Design.Hint">
  <item name="android:fontFamily">@font/my_font</item>
</style>

<style name="TextInputLayoutHelperText" parent="TextAppearance.Design.HelperText">
  <item name="android:fontFamily">@font/my_font</item>
</style>

<style name="TextInputLayoutErrorText" parent="TextAppearance.Design.Error">
  <item name="android:fontFamily">@font/my_font</item>
</style>
<!-- endregion -->
Run Code Online (Sandbox Code Playgroud)

然后在xml布局中:

<android.support.design.widget.TextInputLayout
    style="@style/TextInputLayout.OutlineBox.CustomFont"
    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/first_name"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

这是结果:

在此输入图像描述


Ima*_*ian 8

我刚刚找到一个简单的解决方案,它对我有用:

通过这种方式,您可以将字体设置为任何编辑文本的提示:

在layout.xml中:

 <android.support.design.widget.TextInputLayout
            android:id="@+id/text_input1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/edt_user"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/username"/>
        </android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

在java类中:

public class MainActivity extends AppCompatActivity {

EditText editText;
TextInputLayout textInputLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Typeface font_yekan= Typeface.createFromAsset(getAssets(), "fonts/byekan.ttf");
        textInputLayout= (TextInputLayout) findViewById(R.id.text_input1);
    textInputLayout.setTypeface(font_yekan);
      }
 }
Run Code Online (Sandbox Code Playgroud)

  • 很奇怪,这应该对我有用.我使用自定义文本输入布局,只在使用setTypeface()初始化时更改了字体,并且错误视图仍然使用较旧的字体.这很奇怪,因为在代码中,错误视图也在setTypeface()中使用了字体.任何线索我可能会失踪? (2认同)

Ale*_*lic 6

这是adneal答案的自定义类实现.

public class CustomTextInputLayout extends TextInputLayout {

    public CustomTextInputLayout(Context context) {
        super(context);
        initFont(context);
    }

    public CustomTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initFont(context);
    }

    private void initFont(Context context) {
        final Typeface typeface = Typeface.createFromAsset(
                context.getAssets(), "fonts/YOUR_CUSTOM_FONT.ttf");

        EditText editText = getEditText();
        if (editText != null) {
            editText.setTypeface(typeface);
        }
        try {
            // Retrieve the CollapsingTextHelper Field
            final Field cthf = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper");
            cthf.setAccessible(true);

            // Retrieve an instance of CollapsingTextHelper and its TextPaint
            final Object cth = cthf.get(this);
            final Field tpf = cth.getClass().getDeclaredField("mTextPaint");
            tpf.setAccessible(true);

            // Apply your Typeface to the CollapsingTextHelper TextPaint
            ((TextPaint) tpf.get(cth)).setTypeface(typeface);
        } catch (Exception ignored) {
            // Nothing to do
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的XML文件中,您现在需要使用CustomTextInputLayout而不是,TextInputLayout它将开箱即用.

<your.package.CustomTextInputLayout
    android:id="@+id/textInputLayout_email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <AutoCompleteTextView
        android:id="@+id/editText_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_email"
        android:inputType="textEmailAddress" />
Run Code Online (Sandbox Code Playgroud)

谢谢adneal的答案.


Ali*_*Ali 5

final Typeface tf = Typeface.createFromAsset(getAssets(), "your_custom_font.ttf");
final TextInputLayout til = (TextInputLayout) findViewById(R.id.yourTextInputLayout);
til.getEditText().setTypeface(tf);
til.setTypeface(tf);
Run Code Online (Sandbox Code Playgroud)