我在应用程序中使用Roboto light字体.要设置字体,我必须在每个视图中添加android:fontFamily="sans-serif-light".有没有办法将Roboto字体声明为整个应用程序的默认字体系列?我试过这样做,但似乎不起作用.

<style name="AppBaseTheme" parent="android:Theme.Light"></style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

推荐答案

答案是肯定的.

全球机器人灯光TextView级和Button级:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
    <item name="android:buttonStyle">@style/RobotoButtonStyle</item>
</style>

<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

<style name="RobotoButtonStyle" parent="android:Widget.Holo.Button">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

只需从列表themes.xml中 Select 所需的样式,然后根据原始样式创建自定义样式.最后,应用样式作为应用程序的主题.

<application
    android:theme="@style/AppTheme" >
</application>

它只能使用像Roboto这样的内置字体,但这就是问题所在.For custom fonts (loaded from assets for example) this method will not work.

EDIT 08/13/15

如果使用AppCompat主题,请记住删除android:前缀.例如:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
    <item name="buttonStyle">@style/RobotoButtonStyle</item>
</style>

注意buttonStyle不包含android:前缀,但textViewStyle必须包含它.

Android相关问答推荐

Android 14(Oneui 6)中的本地推送通知行为不一致

无法在Jetpack Compose中显示Admob原生广告

如何在Jetpack composeH中创建具有弯曲末端的六边形形状

在卡片上创建圆角底部边框

MAP函数返回单位列表而不是字符列表

StateFlow和LazyColumn重组

Android在NavHost中的LazyColumn中编写约束布局:error - replace()在未放置的项目上调用

无法加载类';com.android.build.api.extension.AndroidComponentsExtension';

Camera2 将图像从 ImageReader 传递到 MediaRecorder

kotlin中&&和and的区别

在 Android 房间迁移中获取上下文

视觉转换后获取文本

在 react native 中设置 react-native-paper 组件的样式

如何在 Jetpack Compose 中设置卡片高度

如何像 XML 一样在 Compose Android Studio 中折叠/展开小部件代码区域/区域

Android Jetpack Compose - 每次文本字段值更改时,可组合函数都会重新组合

我应该使用 Bluetooth Classic 还是 Bluetooth LE 与我的移动应用程序通信?

单击登录按钮后从应用程序中退出

如何使用jetpack compose实现布局,其中图标在列布局上是绝对位置

为什么我不能在屏幕外拿任何物体