Android 12及以上:如何通过Splash Screen API在启动页Logo下方添加文字?
在Splash Screen API中添加Logo下方文字的可行方案
针对你遇到的「Theme.SplashScreen无文字属性」的问题,以下是几种无需依赖第三方方案的原生实现方式:
方案1:自定义包含文字的Drawable(最简便)
直接通过layer-list将Logo和文字合并为一个Drawable,作为Splash的图标使用,完全兼容原生API:
- 创建文字Drawable(
res/drawable/splash_text.xml):
<text xmlns:android="http://schemas.android.com/apk/res/android" android:text="你的应用名称" android:textSize="18sp" android:textColor="@color/white" android:gravity="center_horizontal" android:paddingTop="100dp"/> <!-- 调整paddingTop让文字处于Logo下方,数值根据Logo高度适配 -->
- 创建合并后的Logo+文字Drawable(
res/drawable/splash_logo_with_text.xml):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 底层放置Logo,居中显示 --> <item> <bitmap android:gravity="center" android:src="@drawable/ic_app_logo"/> </item> <!-- 上层放置文字Drawable --> <item android:drawable="@drawable/splash_text"/> </layer-list>
- 在Splash主题中配置:
<style name="Theme.App.Splash" parent="Theme.SplashScreen"> <item name="windowSplashScreenIcon">@drawable/splash_logo_with_text</item> <item name="windowSplashScreenBackgroundColor">@color/splash_bg</item> <item name="postSplashScreenTheme">@style/Theme.App</item> </style>
方案2:自定义Splash布局(高度灵活)
如果需要动态修改文字、更精细的布局控制,可以直接自定义Splash Activity,绕过原生Logo的限制:
- 创建Splash布局(
res/layout/activity_splash.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/splash_bg" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="120dp" android:layout_height="120dp" android:src="@drawable/ic_app_logo"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="你的应用名称" android:textColor="@color/white" android:textSize="20sp" android:textStyle="bold"/> </LinearLayout>
- 实现Splash Activity:
class SplashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) // 模拟启动加载,延迟跳转到主页面 Handler(Looper.getMainLooper()).postDelayed({ startActivity(Intent(this, MainActivity::class.java)) finish() }, 1500) } }
- 在Manifest中设置为启动Activity,并关联主题:
<activity android:name=".SplashActivity" android:theme="@style/Theme.App.Splash"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
方案3:动态添加文字到原生Splash布局(保留原生特性)
如果想保留Splash Screen API的系统级动画、版本适配特性,可通过代码动态在原生Splash布局中插入文字:
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 初始化原生Splash Screen val splashScreen = installSplashScreen() val splashRootView = splashScreen.view as FrameLayout // 创建文字View并设置位置 val appNameText = TextView(this).apply { text = "你的应用名称" setTextColor(Color.WHITE) textSize = 18f layoutParams = FrameLayout.LayoutParams( FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT ).apply { gravity = Gravity.CENTER_HORIZONTAL or Gravity.TOP // 调整marginTop让文字处于Logo下方,数值根据Logo尺寸适配 topMargin = resources.getDimensionPixelSize(R.dimen.splash_text_margin) } } // 添加到Splash布局 splashRootView.addView(appNameText) // 保留原生退出动画(可选) splashScreen.setOnExitAnimationListener { view -> val exitAnim = ObjectAnimator.ofFloat( view, View.TRANSLATION_Y, 0f, -view.height.toFloat() ) exitAnim.interpolator = AnticipateInterpolator() exitAnim.duration = 500L exitAnim.doOnEnd { view.remove() } exitAnim.start() } setContentView(R.layout.activity_main) } }
内容的提问来源于stack exchange,提问作者Sakthivel K
相关产品推荐
相关产品推荐

