Android Studio Electric Eel使用自定义字体时预览渲染异常问题
解决Android Studio自定义字体导致布局预览失效的问题
问题背景
在Android Studio Dolphin及升级到Electric Eel版本后,项目中使用自定义字体时布局预览无法正常渲染,移除TextView的android:fontFamily="@font/quicksand_bold"属性后预览恢复正常。
可行修复方案
替换字体引用属性
将TextView中的android:fontFamily="@font/quicksand_bold"替换为app:fontFamily="@font/quicksand_bold",AppCompat库对该属性的预览兼容性更好,真机和预览都能正常渲染字体。检查字体文件与配置
确认res/font目录下的字体文件(ttf/otf)无损坏,文件名不含空格、特殊字符;如果使用字体族配置xml文件,确保格式正确,示例如下:<font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="700" android:font="@font/quicksand_bold_ttf"/> </font-family>清除Studio缓存
依次点击File > Invalidate Caches... > Invalidate and Restart,重启后重新加载项目,多数预览异常问题可通过此操作解决。临时降级AppCompat库
若上述方法无效,可尝试将build.gradle中AppCompat库版本降至1.4.2,部分新版本库与Studio预览工具存在适配冲突。
修复后示例布局代码
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" app:fontFamily="@font/quicksand_bold" android:gravity="center" android:text="@string/settings" android:textColor="@color/colorWhite" android:textSize="28sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <View android:id="@+id/view" android:layout_width="match_parent" android:layout_height="0.1dp" android:layout_marginStart="24dp" android:layout_marginEnd="24dp" android:background="@color/colorSecondary" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者Ahamed Lincon
相关产品推荐
相关产品推荐

