为何targetSdkVersion 30时Android页面不显示文本,28时正常?
Hey there, let's break down why your text vanishes when targeting API 30 (Android 11) but shows up fine on API 28. I've dealt with similar SDK version quirks before, so here are the most probable causes and how to debug them:
1. 分区存储限制导致字体加载失败
Starting with API 30, Android enforces Scoped Storage by default, which restricts access to external storage. If your text uses custom fonts loaded from external files (like SD card), your app might no longer be able to read those files in API 30—resulting in invisible text since the font can't be loaded.
- Debug steps:
- Check Logcat for
FileNotFoundExceptionor font-related error messages - Try moving font files to your app's private storage (e.g.,
getFilesDir()) instead of external storage - As a temporary fix, add
android:requestLegacyExternalStorage="true"to your<application>tag inAndroidManifest.xml(note: this is not a long-term solution—you should adapt to Scoped Storage eventually)
- Check Logcat for
2. View绘制或层级行为变化
Android 10+ introduced subtle tweaks to how views are rendered. For example, changes to hardware acceleration defaults, view visibility handling, or color rendering could make text appear invisible even though it's technically present.
- Debug steps:
- Use Android Studio's Layout Inspector to compare the view hierarchy between API 28 and 30. Check if the text view has an unexpected
visibilityvalue,alpha=0, or text color matching the background. - Disable hardware acceleration for the problematic activity by adding
android:hardwareAccelerated="false"to its<activity>tag in the manifest, then test if text reappears.
- Use Android Studio's Layout Inspector to compare the view hierarchy between API 28 and 30. Check if the text view has an unexpected
3. Dark theme auto-switching causing contrast issues
API 29+ improved dark theme support, and if your app uses a DayNight-compatible theme, it might automatically switch to dark mode on API 30. If your text color isn't properly adjusted for dark themes, it could blend into the background (e.g., white text on a white background in light mode, but dark text on dark background in auto-switched dark mode).
- Debug steps:
- Force your app to use light mode by adding
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)in your activity'sonCreate()method beforesetContentView(), then test. - Check your theme's text color attributes—ensure they use theme-aware resources like
?attr/textColorPrimaryinstead of hardcoded values.
- Force your app to use light mode by adding
4. Font fallback behavior changes
Android 11 adjusted how system fonts fall back when a custom font is missing certain characters. If your custom font doesn't include all the characters in your text, API 30 might not fall back to the system font as gracefully as API 28, leading to blank spaces instead of text.
- Debug steps:
- Replace your custom font with a system default (e.g.,
android:fontFamily="sans-serif") and see if the text displays correctly. - Verify your custom font file includes all the characters used in your text (use a font viewer tool to check the character set).
- Replace your custom font with a system default (e.g.,
内容的提问来源于stack exchange,提问作者Mostafa khaled

