AndroidManifest.xml
android:theme="@style/AppTheme"styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:fontFamily">@font/montserrat</item>
<item name="fontFamily">@font/montserrat</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>activity_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="@layout/content_dashboard" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/background_top_right_radius_white"
android:fitsSystemWindows="false"
app:headerLayout="@layout/nav_header">
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_160sdp"
android:divider="@color/white"
android:dividerHeight="0dp"
android:groupIndicator="@null" />
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>content_dashboard.xml (仅共享文本视图代码)
<TextView
android:id="@+id/tvDashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_45sdp"
android:layout_marginTop="@dimen/_10sdp"
android:text="@string/dashboard"
android:textColor="@color/black"
android:textSize="@dimen/_20sdp"
android:textStyle="bold" /> //this is not workingDashboard.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
TextView tvDashboard = findViewById(R.id.tvDashboard);
tvDashboard.setTypeface(null, Typeface.BOLD); //this is not working
}使用https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts#via-android-studio将整个应用程序字体设置为蒙特色拉特语。但是,在某些部分我有标题之类的,所以我想把样式改成粗体。当我使用setTypeface代码时,它在某些地方可以工作,但在其他地方不能工作(包括上面的例子)。这很奇怪,我不确定它为什么会这样。
发布于 2020-04-10 00:09:04
要创建字体系列,请在Android Studio中执行以下步骤:
右键单击字体文件夹,然后转到新建>字体资源文件。此时将出现“New Resource File”窗口。输入文件名,然后单击确定。新的字体资源XML将在编辑器中打开。将每个字体文件、样式和粗细属性包含在元素中。以下XML说明了如何在字体资源XML中添加与字体相关的属性:
<?xml version="1.0" encoding="UTF-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular" />
<font app:fontStyle="bold" app:fontWeight="400" app:font="@font/myfont-Bold" />
<font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont-Italic" />
</font-family>现在在文本视图中使用它作为
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:fontFamily="@font/font-family"/>https://stackoverflow.com/questions/61125144
复制相似问题