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
.
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)
错误视图
如果您需要更改错误的字体,可以执行以下两项操作之一:
TextView
并Typeface
像以前一样应用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.
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)
这是结果:
我刚刚找到一个简单的解决方案,它对我有用:
通过这种方式,您可以将字体设置为任何编辑文本的提示:
在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)
这是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的答案.
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)
归档时间: |
|
查看次数: |
21289 次 |
最近记录: |