Jemand hat versucht, die Schriftart des Floating-Labels zu ändern? Ich habe die Quelle von EditText geändert, aber die Schriftart des Floating-Labels hat sich nicht geändert. Ich bin denjenigen sehr dankbar, die mir helfen
Code:
<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));
Ab Design Library v23
können Sie TextInputLayout#setTypeface()
verwenden.
Dadurch wird die Schriftart sowohl für den erweiterten als auch für den gleitenden Hinweis festgelegt.
Hier ist die Feature-Anfrage wo sie auf b.Android.com diskutiert wurde.
EDIT: Die Schriftart für die Fehleransicht wurde nicht festgelegt, ist aber jetzt behoben in v25.1.0
.
Leider müssen Sie die Reflektion verwenden, um damit umzugehen.
Das Floating-Label wird von CollapsingTextHelper
gezeichnet, einer internen Klasse für private Pakete, die nicht für die Verarbeitung von Bereichen konfiguriert ist. Die Verwendung einer benutzerdefinierten Variable TypefaceSpan
funktioniert in diesem Fall also nicht.
Da dies Reflexion verwendet, ist es in Zukunft nicht garantiert, dass es funktioniert.
Implementierung
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
}
Fehleransicht
Wenn Sie die Schriftart des Fehlers ändern mussten, haben Sie zwei Möglichkeiten:
TextView
auf und wenden Sie die Typeface
wie zuvor anTextInputLayout
verwendete Fehleransicht nur eine TextView
, sodass Spannweiten verarbeitet werden können.Reflektion verwenden
final Field errorField = til.getClass().getDeclaredField("mErrorView");
errorField.setAccessible(true);
((TextView) errorField.get(til)).setTypeface(tf);
Verwenden eines benutzerdefinierten Bereichs
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);
}
}
Ergebnisse
Die Schriftart, die ich verwende, ist Smoothie Shoppe .
ich habe gerade eine einfache Lösung gefunden und sie hat für mich funktioniert:
auf diese Weise können Sie die Schriftart so einstellen, dass sie auf einen beliebigen Bearbeitungstext verweist:
in 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>
und in Java-Klasse:
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);
}
}
Hier ist eine benutzerdefinierte Klassenimplementierung für die Antwort von 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
}
}
}
In Ihren XML-Dateien müssen Sie jetzt CustomTextInputLayout
anstelle von TextInputLayout
verwenden, und es funktioniert sofort.
<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" />
Danke adneal für die Antwort.
Ich verwende das neue MaterialComponents
- Thema und keine der Antworten hat mir geholfen.
Musste selbst mit Stilen und Themen spielen. Wird hier ein Stück Stil posten, falls jemand das gleiche Problem hat.
<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 -->
Dann im XML-Layout:
<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>
Hier ist das Ergebnis:
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);
behebung eines Problems in der @adneal-Antwort: Wenn setErrorEnabled nicht auf true gesetzt ist, wäre mErrorView null und wenn Sie es zu einem beliebigen Zeitpunkt auf false setzen, wird die Schriftart wieder in den Standardwert ... zurückgesetzt.
in Ihrem benutzerdefinierten TextInputLayout überschreiben Sie setErrorEnabled
@Override
public void setErrorEnabled(boolean enabled) {
super.setErrorEnabled(enabled);
if (enabled) {
try {
Field cthf = TextInputLayout.class.getDeclaredField("mErrorView");
cthf.setAccessible(true);
TextView error = (TextView) cthf.get(this);
if (error != null)
error.setTypeface(tf);
} catch (Exception e) {
}
}
}
Es gibt einen einfacheren Weg,
Erstellen Sie in Ihrem 'res' -Ordner ein neues Verzeichnis mit dem Namen 'font' und fügen Sie dort eine Schriftart ein. Dann öffne deine 'Styles' Datei und erstelle einen neuen Style:
<style name="customfontstyle" parent="@Android:style/TextAppearance.Small">
<item name="Android:fontFamily">@font/poppins_regular</item>
</style>
Sie können auch weitere Eigenschaften wie textColor, textSize usw. hinzufügen.
Dann in Ihrem XML:
<Android.support.design.widget.TextInputLayout
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
app:hintTextAppearance="@style/customfontstyle"
>
<Android.support.design.widget.TextInputEditText
Android:layout_width="220dp"
Android:layout_height="wrap_content"
Android:id="@+id/edit_phone_number"
Android:hint="@string/phone_number_label"
Android:inputType="number"
/>
</Android.support.design.widget.TextInputLayout>
Ich habe es überprüft und es funktioniert.
Ich suchte nach diesem, ich fand diesen Weg, indem ich die Unterstützungsbibliothek verwendete:
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
und setzen Sie diese Schriftart auf TextInpuLayout.
Für mich wirkt wie Charme, ich hoffe es hilft anderen =]
Quelle: Dokumentation
So komme ich dazu
edit_login_emailOrPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
textInputLayout_login_emailOrPhone.setTypeface(APSApplication.getInstance().getFonts().getTypefaceSemiBold());
}else
{
textInputLayout_login_emailOrPhone.setTypeface(APSApplication.getInstance().getFonts().getTypefaceRegular());
}
}
});
Verwenden Sie style.xml wie folgt:
Style-Datei:
<style name="TextInputLayoutErrorStyle" parent="TextAppearance.Design.Error">
<item name="fontFamily">@font/iran_sans_medium</item>
<item name="Android:fontFamily">@font/iran_sans_medium</item>
</style>
<style name="TextInputLayoutHintStyle" parent="TextAppearance.Design.Hint">
<item name="fontFamily">@font/iran_sans_medium</item>
<item name="Android:fontFamily">@font/iran_sans_medium</item>
</style>
<style name="TextInputLayoutHelperStyle" parent="TextAppearance.Design.HelperText">
<item name="fontFamily">@font/iran_sans_medium</item>
<item name="Android:fontFamily">@font/iran_sans_medium</item>
</style>
<style name="TextInputLayoutOutlinedBoxStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="helperTextTextAppearance">@style/TextInputLayoutHelperStyle</item>
<item name="errorTextAppearance">@style/TextInputLayoutErrorStyle</item>
<item name="hintTextAppearance">@style/TextInputLayoutHintStyle</item>
</style>
Layoutdatei:
<com.google.Android.material.textfield.TextInputLayout
Android:layout_width="match_parent"
Android:layout_centerInParent="true"
Android:hint="@string/cardname_hint"
Android:layout_marginStart="30dp"
Android:layout_marginEnd="30dp"
card_view:helperText="@string/cardname_helper"
style="@style/TextInputLayoutOutlinedBoxStyle"
Android:layout_height="wrap_content">
<com.google.Android.material.textfield.TextInputEditText
Android:layout_width="match_parent"
Android:fontFamily="@font/iran_sans_medium"
Android:textColor="@color/colorTextPrimary"
Android:layout_height="wrap_content" />
</com.google.Android.material.textfield.TextInputLayout>