首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在Android Studio中通过xml或以编程方式将粗体样式设置为TextView

无法在Android Studio中通过xml或以编程方式将粗体样式设置为TextView
EN

Stack Overflow用户
提问于 2020-04-09 23:53:28
回答 1查看 165关注 0票数 0

AndroidManifest.xml

代码语言:javascript
复制
android:theme="@style/AppTheme"

styles.xml

代码语言:javascript
复制
 <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

代码语言:javascript
复制
<?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 (仅共享文本视图代码)

代码语言:javascript
复制
<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 working

Dashboard.java

代码语言:javascript
复制
@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代码时,它在某些地方可以工作,但在其他地方不能工作(包括上面的例子)。这很奇怪,我不确定它为什么会这样。

EN

回答 1

Stack Overflow用户

发布于 2020-04-10 00:09:04

要创建字体系列,请在Android Studio中执行以下步骤:

右键单击字体文件夹,然后转到新建>字体资源文件。此时将出现“New Resource File”窗口。输入文件名,然后单击确定。新的字体资源XML将在编辑器中打开。将每个字体文件、样式和粗细属性包含在元素中。以下XML说明了如何在字体资源XML中添加与字体相关的属性:

代码语言:javascript
复制
<?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>

现在在文本视图中使用它作为

代码语言:javascript
复制
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:fontFamily="@font/font-family"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61125144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档