问题
I want to change the font of my chronometer in android.I have set my default font to be a custom font . The font seems to be applied every where except the chronometer. I have also tried setting android:fontFamily="@font/myfont"
but it didn't work. Though the font appears to be applied in the preview window, but it doesn't change when i run my app on my device.
Any idea to get over it ?
Here's my code:
<Chronometer
android:fontFamily="@font/linotte_semibold"
android:id="@+id/audio_player_chronometer"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="@+id/frame_layout"
android:textColor="@color/white"
android:layout_margin="5dp"
/>
回答1:
You can download Fonts from Google Material
- Go to layout editor
- Select your
view
and viewAll Attributes
on your right side and findfontFamily
- Now click
down arrow
, it will open a window for selecting font
Find your desired font, you can also search and click
ok
Your
Chronometer
will look differently
Here is the code
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:fontFamily="@font/allan_bold"
android:textColor="@color/colorAccent"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
If in case font does not change use this in your Activity.class
Chronometer chronometer = findViewById(R.id.chronometer);
chronometer.setTypeface(ResourcesCompat.getFont(this, R.font.allan_bold));
//replace allan_bold with your desired font
Edit 1 Here is the Output in the device
Hope this will help!
回答2:
I just follow this to add font to Android https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
- Copy .ttf / .otf file to "res/font" folder
- Generate font resource file with style and weight details.
- Set new font as
android:fontFamily="@font/sample_font"
Check sample code below, with comparison with and without adding font
<!--With Font-->
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/framelayout"
android:layout_margin="5dp"
android:fontFamily="@font/sample_font"
android:textColor="@android:color/holo_blue_dark"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/audio_player_chronometer"
app:layout_constraintVertical_bias="0.528" />
<!--Without Font-->
<Chronometer
android:id="@+id/audio_player_chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/framelayout"
android:layout_margin="5dp"
android:textSize="30sp"
android:textColor="@android:color/holo_blue_dark"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/framelayout" />
<!--sample text-->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:fontFamily="@font/sample_font"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Screenshot
回答3:
You can change the font style and font size on chronometer using following
XML
android:typeface="serif"
android:textSize="120dp"
in xml
you can set default fonts of android.
JAVA
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/linotte_semibold.ttf");
focus = (Chronometer) findViewById(R.id.chronometer1);
focus.setTypeface(font, Typeface.NORMAL);
focus.setTextSize(120);
where focus is chronometer.
for custom fonts
put your .ttf files in assets folder.
in android studio you have to create assets folder under src/main/assets/
来源:https://stackoverflow.com/questions/59039369/chronometer-font-doesnt-change-in-android