A implementação de uma visão de texto e todas as suas subclasses diretas/indiretas com suporte nativo para as fontes Roboto, inclui as novas fontes Roboto e Roboto Mono.
Fontes suportadas:
Este projeto está preguiçoso em favor da implementação nativa introduzida no Android O.
Esta biblioteca é compatível na 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 > Para configurar um tipo de letra, você deve especificar o parâmetro 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 especifique um conjunto de parâmetros robotoFontFamily , robotoTextWeight e 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 " />Configure o tipo de letra no código:
Usando typeface de parâmetro:
RobotoTextView textView = new RobotoTextView ( context );
RobotoTypefaces . setUpTypeface (
textView ,
RobotoTypefaces . TYPEFACE_ROBOTO_LIGHT_ITALIC ); Usando parâmetros fontFamily , textWeight e textStyle :
RobotoTextView textView = new RobotoTextView ( context );
RobotoTypefaces . setUpTypeface (
textView ,
RobotoTypefaces . FONT_FAMILY_ROBOTO ,
RobotoTypefaces . TEXT_WEIGHT_LIGHT ,
RobotoTypefaces . TEXT_STYLE_ITALIC ); Usando typeface de parâmetro:
RobotoTypefaceSpan span = new RobotoTypefaceSpan (
context ,
RobotoTypefaces . TYPEFACE_ROBOTO_LIGHT_ITALIC );
Spannable spannable = new SpannableString ( "text" );
spannable . setSpan ( span , 0 , 1 , Spannable . SPAN_EXCLUSIVE_EXCLUSIVE ); Usando parâmetros fontFamily , textWeight e 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 ); Para aplicar o tipo de letra Roboto para o TextView original (como uma caligrafia), você deve anexar o InfLater em sua classe Activity no método #onCreate() .
@ Override
protected void onCreate ( @ Nullable Bundle savedInstanceState ) {
RobotoInflater . attach ( this );
super . onCreate ( savedInstanceState );
}e especifique o tipo de letra em 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 " /> Nota: O IDE popular (Android Studio, Intellij) provavelmente marcará isso como um erro, apesar de estar correto. Você pode adicionar tools:ignore="MissingPrefix" à própria exibição ou ao seu grupo de vista para os pais para evitar isso. Você precisará adicionar o espaço para nome das ferramentas para ter acesso a esse atributo "ignorar". xmlns:tools=" http://schemas.android.com/tools" . Consulte https://code.google.com/p/android/issues/detail?id=65176.
Se você pretende usar nem todas as fontes, as fontes extras podem ser removidas.
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.