Die Implementierung eines Textviews und aller direkten/indirekten Unterklassen mit nativem Unterstützung für die Roboto -Schriftarten umfasst die brandneuen Roboto -Slab- und Roboto -Mono -Schriftarten.
Unterstützte Schriftarten:
Dieses Projekt ist zugunsten der in Android O eingeführten nativen Implementierung veraltet.
Diese Bibliothek ist aus API 14 (Android 4.0) kompatibel.
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 > Zum Einrichten einer Schrift müssen Sie den Parameter robotoTypeface angeben:
< 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 " /> Oder geben Sie eine Reihe von Parametern an, robotoFontFamily , robotoTextWeight und 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 " />Setzen Sie die Schrift im Code ein:
Verwenden der typeface :
RobotoTextView textView = new RobotoTextView ( context );
RobotoTypefaces . setUpTypeface (
textView ,
RobotoTypefaces . TYPEFACE_ROBOTO_LIGHT_ITALIC ); Verwenden von Parametern fontFamily , textWeight und textStyle :
RobotoTextView textView = new RobotoTextView ( context );
RobotoTypefaces . setUpTypeface (
textView ,
RobotoTypefaces . FONT_FAMILY_ROBOTO ,
RobotoTypefaces . TEXT_WEIGHT_LIGHT ,
RobotoTypefaces . TEXT_STYLE_ITALIC ); Verwenden der typeface :
RobotoTypefaceSpan span = new RobotoTypefaceSpan (
context ,
RobotoTypefaces . TYPEFACE_ROBOTO_LIGHT_ITALIC );
Spannable spannable = new SpannableString ( "text" );
spannable . setSpan ( span , 0 , 1 , Spannable . SPAN_EXCLUSIVE_EXCLUSIVE ); Verwenden von Parametern fontFamily , textWeight und 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 ); Um die Roboto -Schrift für die Original -Textansicht (wie eine Kalligraphie) anzuwenden, müssen Sie Inflater in Ihrer Activity in der Methode #onCreate() anschließen.
@ Override
protected void onCreate ( @ Nullable Bundle savedInstanceState ) {
RobotoInflater . attach ( this );
super . onCreate ( savedInstanceState );
}und geben Sie die Schrift in XML an:
< 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 " /> Hinweis: Das beliebte IDEs (Android Studio, Intellij) wird dies wahrscheinlich als Fehler markieren, obwohl er korrekt ist. Möglicherweise möchten Sie tools:ignore="MissingPrefix" entweder der Ansicht selbst oder der übergeordneten ViewGroup, um dies zu vermeiden. Sie müssen den Tools -Namespace hinzufügen, um Zugriff auf dieses "Ignorieren" -Merkmal zu erhalten. xmlns:tools=" http://schemas.android.com/tools" . Siehe https://code.google.com/p/android/issues/detail?id=65176.
Wenn Sie nicht alle Schriftarten verwenden möchten, können die zusätzlichen Schriftarten entfernt werden.
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.