Implementasi TextView dan semua subclass langsung/tidak langsung dengan dukungan asli untuk roboto font, termasuk roboto slab baru dan roboto mono font.
Font yang Didukung:
Proyek ini sudah usang mendukung implementasi asli yang diperkenalkan di Android O.
Perpustakaan ini kompatibel dari 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 > Untuk mengatur jenis huruf, Anda harus menentukan parameter 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 " /> atau tentukan satu set parameter robotoFontFamily , robotoTextWeight dan 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 " />Mengatur jenis huruf dalam kode:
Menggunakan typeface parameter:
RobotoTextView textView = new RobotoTextView ( context );
RobotoTypefaces . setUpTypeface (
textView ,
RobotoTypefaces . TYPEFACE_ROBOTO_LIGHT_ITALIC ); Menggunakan Parameter fontFamily , textWeight dan textStyle :
RobotoTextView textView = new RobotoTextView ( context );
RobotoTypefaces . setUpTypeface (
textView ,
RobotoTypefaces . FONT_FAMILY_ROBOTO ,
RobotoTypefaces . TEXT_WEIGHT_LIGHT ,
RobotoTypefaces . TEXT_STYLE_ITALIC ); Menggunakan typeface parameter:
RobotoTypefaceSpan span = new RobotoTypefaceSpan (
context ,
RobotoTypefaces . TYPEFACE_ROBOTO_LIGHT_ITALIC );
Spannable spannable = new SpannableString ( "text" );
spannable . setSpan ( span , 0 , 1 , Spannable . SPAN_EXCLUSIVE_EXCLUSIVE ); Menggunakan Parameter fontFamily , textWeight dan 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 ); Untuk menerapkan jenis huruf Roboto untuk TextView asli (seperti kaligrafi), Anda harus melampirkan inflater di kelas Activity Anda dalam metode #onCreate() .
@ Override
protected void onCreate ( @ Nullable Bundle savedInstanceState ) {
RobotoInflater . attach ( this );
super . onCreate ( savedInstanceState );
}dan tentukan jenis huruf di 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 " /> Catatan: IDE populer (Android Studio, IntelliJ) kemungkinan akan menandai ini sebagai kesalahan meskipun benar. Anda mungkin ingin menambahkan tools:ignore="MissingPrefix" ke tampilan itu sendiri atau kelompok view induknya untuk menghindari ini. Anda harus menambahkan namespace alat untuk memiliki akses ke atribut "abaikan" ini. xmlns:tools=" http://schemas.android.com/tools" . Lihat https://code.google.com/p/android/issues/detail?id=65176.
Jika Anda ingin menggunakannya tidak semua font, font tambahan dapat dihapus.
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.