Tipo de letra personalizado

Estoy usando nuevas características de piruletas como coloraccent, colorPrimary en estilos para dispositivos pre-lollipop.

Styles.xml

<resources> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/material_teal500</item> <item name="colorPrimaryDark">@color/material_teal600</item> <item name="colorAccent">@color/material_white</item> </style> 

Ahora quiero crear un tipo de letra personalizado para mi textview, edittext y botón. En estilos estoy usando coloraccent como blanco . Así que en el enfoque de color blanco edittext debe venir. Consulte la imagen de abajo para el enfoque predeterminado de edittext. Esto funciona bien.

Esta es la imagen de default edittext behavious

Pero siempre que creo un tipo de letra personalizado edittext el comportamiento de enfoque es diferente. No está mostrando el color blanco. En cambio, se muestra en color negro. Vea la siguiente imagen para mi comportamiento edittext personalizado para el correo electrónico.

Este es mi comportamiento edittext personalizado

Esta es mi clase de tipo de letra edittext personalizada.

 public class TypefaceButton extends Button { /* * Permissible values ​​for the "typeface" attribute. */ private final static int ROBOTO_THIN = 0; private final static int ROBOTO_THIN_ITALIC = 1; private final static int ROBOTO_LIGHT = 2; private final static int ROBOTO_LIGHT_ITALIC = 3; private final static int ROBOTO_REGULAR = 4; private final static int ROBOTO_ITALIC = 5; private final static int ROBOTO_MEDIUM = 6; private final static int ROBOTO_MEDIUM_ITALIC = 7; private final static int ROBOTO_BOLD = 8; private final static int ROBOTO_BOLD_ITALIC = 9; private final static int ROBOTO_BLACK = 10; private final static int ROBOTO_BLACK_ITALIC = 11; private final static int ROBOTO_CONDENSED = 12; private final static int ROBOTO_CONDENSED_ITALIC = 13; private final static int ROBOTO_CONDENSED_BOLD = 14; private final static int ROBOTO_CONDENSED_BOLD_ITALIC = 15; /** * List of created typefaces for later reused. */ private final static SparseArray<Typeface> mTypefaces = new SparseArray<Typeface>(16); /** * Simple constructor to use when creating a view from code. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. */ public TypefaceButton(Context context) { super(context); } /** * Constructor that is called when inflating a view from XML. This is called * when a view is being constructed from an XML file, supplying attributes * that were specified in the XML file. This version uses a default style of * 0, so the only attribute values applied are those in the Context's Theme * and the given AttributeSet. * <p/> * <p/> * The method onFinishInflate() will be called after all children have been * added. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. * @see #TypefaceButton(android.content.Context, android.util.AttributeSet, int) */ public TypefaceButton(Context context, AttributeSet attrs) { super(context, attrs); parseAttributes(context, attrs); } /** * Perform inflation from XML and apply a class-specific base style. This * constructor of View allows subclasses to use their own base style when * they are inflating. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. * @param defStyle The default style to apply to this view. If 0, no style * will be applied (beyond what is included in the theme). This may * either be an attribute resource, whose value will be retrieved * from the current theme, or an explicit style resource. * @see #TypefaceButton(android.content.Context, android.util.AttributeSet) */ public TypefaceButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); parseAttributes(context, attrs); } /** * Parse the attributes. * * @param context The Context the view is running in, through which it can access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. */ private void parseAttributes(Context context, AttributeSet attrs) { TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.TypefaceTextView); int typefaceValue = values.getInt(R.styleable.TypefaceTextView_typeface, 0); values.recycle(); setTypeface(obtaintTypeface(context, typefaceValue)); } /** * Obtain typeface. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param typefaceValue values ​​for the "typeface" attribute * @return Roboto {@link android.graphics.Typeface} * @throws IllegalArgumentException if unknown `typeface` attribute value. */ private Typeface obtaintTypeface(Context context, int typefaceValue) throws IllegalArgumentException { Typeface typeface = mTypefaces.get(typefaceValue); if (typeface == null) { typeface = createTypeface(context, typefaceValue); mTypefaces.put(typefaceValue, typeface); } return typeface; } /** * Create typeface from assets. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param typefaceValue values ​​for the "typeface" attribute * @return Roboto {@link android.graphics.Typeface} * @throws IllegalArgumentException if unknown `typeface` attribute value. */ private Typeface createTypeface(Context context, int typefaceValue) throws IllegalArgumentException { Typeface typeface; switch (typefaceValue) { case ROBOTO_THIN: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Thin.ttf"); break; case ROBOTO_THIN_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-ThinItalic.ttf"); break; case ROBOTO_LIGHT: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf"); break; case ROBOTO_LIGHT_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-LightItalic.ttf"); break; case ROBOTO_REGULAR: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"); break; case ROBOTO_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Italic.ttf"); break; case ROBOTO_MEDIUM: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"); break; case ROBOTO_MEDIUM_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-MediumItalic.ttf"); break; case ROBOTO_BOLD: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); break; case ROBOTO_BOLD_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldItalic.ttf"); break; case ROBOTO_BLACK: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"); break; case ROBOTO_BLACK_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BlackItalic.ttf"); break; case ROBOTO_CONDENSED: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Condensed.ttf"); break; case ROBOTO_CONDENSED_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-CondensedItalic.ttf"); break; case ROBOTO_CONDENSED_BOLD: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensed.ttf"); break; case ROBOTO_CONDENSED_BOLD_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf"); break; default: throw new IllegalArgumentException("Unknown `typeface` attribute value " + typefaceValue); } return typeface; } } 

Utilicé la cara de encargo del tipo en xml como siguiente.

 <appname.utilities.TypefaceEditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="@dimen/ten_dp" custom:typeface="roboto_light" android:ems="10" android:hint="@string/user_email" android:inputType="textEmailAddress" android:textColor="@color/material_grey200" android:textColorHint="@color/material_grey200" /> 

¡Cómo puedo resolver esto! ¿Alguna idea? Sé que puedo implementar la fuente de encargo en archivo de la clase de java. Pero me gustaría implementar de esta manera. En mi propio texto de edición personalizado también quiero mostrar el mismo color de acento. Gracias en antes!

¡Lo encontré! Para aplicar colorAccent, simplemente extienda su clase personalizada EditText de android.support.v7.widget.AppCompatEditText

Esto está trabajando para mi edittext personalizado, vamos a probar este código para su caso. Agregue las 2 líneas de XML en Activity's diseño de su Activity's (diseño de su edittext ):

 <LinearLayout android:focusable="true" android:focusableInTouchMode="false" /> 

Intente algo como esto:

 editText2.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { editText2.setFocusableInTouchMode(true); return false; } }); 
  • ¿Cómo recuperar partes del contenido de Wikipedia en la aplicación para Android?
  • Error: Los tipos de cadena no están permitidos (en 'layout_height' con el valor 'wrap content'). activity_main.xml
  • ¿Es posible hacer un ListView poblar desde la parte inferior?
  • Android Studio - Importación de la biblioteca Simple-Xml
  • Iconos aplastados en la barra de herramientas después de cambiar a Android SDK 26
  • Andorid cordova phonegap config.xml prefijo sin conexión
  • Android: Obtener todas las entradas seleccionadas de MultiSelectListPreference (SharedPreferences)
  • Parser SAX vs analizador XMLPull
  • Uso de attrs personalizados en styles.xml en android
  • El signo de interrogación me parece nuevo
  • ¿Cómo puedo convertir un diseño PSD a Android xml?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.