La mise en œuvre d'un TextView et de toutes ses sous-classes directes / indirectes avec la prise en charge native pour les polices Roboto, comprend la toute nouvelle dalle Roboto et Roboto Mono Fonts.
Polices prises en charge:
Ce projet est déprécié en faveur de l'implémentation native introduite dans Android O.
Cette bibliothèque est compatible à partir de l'API 14 (Android 4.0).
Gradle:
compile ' com.github.johnkil.android-robototextview:robototextview:4.0.0 'Maven:
< dependency >
< groupId >com.github.johnkil.android-robototextview</ groupId >
< artifactId >robototextview</ artifactId >
< version >4.0.0</ version >
< type >aar</ type >
</ dependency > Pour configurer une police, vous devez spécifier le paramètre robotoTypeface :
< com .devspark.robototextview.widget.RobotoTextView
xmlns : android = " http://schemas.android.com/apk/res/android "
xmlns : app = " http://schemas.android.com/apk/res-auto "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
app : robotoTypeface = " roboto_light_italic " /> ou spécifiez un ensemble de paramètres robotoFontFamily , robotoTextWeight et robotoTextStyle :
< com .devspark.robototextview.widget.RobotoTextView
xmlns : android = " http://schemas.android.com/apk/res/android "
xmlns : app = " http://schemas.android.com/apk/res-auto "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
app : robotoFontFamily = " roboto "
app : robotoTextWeight = " light "
app : robotoTextStyle = " italic " />Configurer la police de type dans le code:
Utilisation de typeface paramètre:
RobotoTextView textView = new RobotoTextView ( context );
RobotoTypefaces . setUpTypeface (
textView ,
RobotoTypefaces . TYPEFACE_ROBOTO_LIGHT_ITALIC ); Utilisation des paramètres fontFamily , textWeight et textStyle :
RobotoTextView textView = new RobotoTextView ( context );
RobotoTypefaces . setUpTypeface (
textView ,
RobotoTypefaces . FONT_FAMILY_ROBOTO ,
RobotoTypefaces . TEXT_WEIGHT_LIGHT ,
RobotoTypefaces . TEXT_STYLE_ITALIC ); Utilisation de typeface paramètre:
RobotoTypefaceSpan span = new RobotoTypefaceSpan (
context ,
RobotoTypefaces . TYPEFACE_ROBOTO_LIGHT_ITALIC );
Spannable spannable = new SpannableString ( "text" );
spannable . setSpan ( span , 0 , 1 , Spannable . SPAN_EXCLUSIVE_EXCLUSIVE ); Utilisation des paramètres fontFamily , textWeight et textStyle :
RobotoTypefaceSpan span = new RobotoTypefaceSpan (
context ,
RobotoTypefaces . FONT_FAMILY_ROBOTO ,
RobotoTypefaces . TEXT_WEIGHT_LIGHT ,
RobotoTypefaces . TEXT_STYLE_ITALIC );
Spannable spannable = new SpannableString ( "text" );
spannable . setSpan ( span , 0 , 1 , Spannable . SPAN_EXCLUSIVE_EXCLUSIVE ); Pour appliquer une police roboto pour TextView d'origine (comme une calligraphie), vous devez joindre un inflatissement dans votre classe Activity dans la méthode #onCreate() .
@ Override
protected void onCreate ( @ Nullable Bundle savedInstanceState ) {
RobotoInflater . attach ( this );
super . onCreate ( savedInstanceState );
}et spécifiez la police dans XML:
< TextView
xmlns : android = " http://schemas.android.com/apk/res/android "
xmlns : app = " http://schemas.android.com/apk/res-auto "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
app : robotoTypeface = " roboto_light_italic "
tools : ignore = " MissingPrefix " /> Remarque: les IDE populaires (Android Studio, IntelliJ) marquent probablement cela comme une erreur malgré le fait d'être correct. Vous voudrez peut-être ajouter tools:ignore="MissingPrefix" à la vue elle-même ou à son groupe de vue parent pour éviter cela. Vous devrez ajouter l'espace de noms d'outils pour avoir accès à cet attribut "ignorer". xmlns:tools=" http://schemas.android.com/tools" . Voir https://code.google.com/p/android/issues/detail?id=65176.
Si vous avez l'intention d'utiliser n'est pas toutes les polices, les polices supplémentaires peuvent être supprimées.
android . applicationVariants . all{ variant ->
variant . mergeAssets . doLast {
File fonts = file( " $v ariant . mergeAssets . outputDir /fonts " )
if (fonts . exists()) {
for ( File file : fonts . listFiles()) {
if (file . getName() . contains( " RobotoSlab " )) {
println ( " delete " + file . getName() + " font " )
file . delete()
}
}
}
}
} Copyright 2014 Evgeny Shishkin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.