骨架图有多种实现方案,这里记录通过png背景图片和theme实现的一种方式。
- 准备和实际Activity大小相同的背景图片,比如img_app_skeleton.png
- 修改values/themes.xml
-
<resources xmlns:tools="http://schemas.android.com/tools"> <style name="AppTheme1" parent="xxx"/> <style name="AppTheme2" parent="xxx"> <item name="android:windowBackground">@drawable/img_app_skeleton</item> </style> </resources>
- 修改AndroidManifest.xml文件中theme标签,将默认theme修改为包含骨架图的theme
-
android:theme="@style/AppTheme2"
- 在你的MainActivity.java中的onCreate()中增加代码:
-
setTheme(R.style.AppTheme2);
- 注意需要添加在setContentView()之前
- 最后adb shell am force-stop xxx ,kill掉app后,运行验证效果