英文:
SplashScreen is center cropping
问题
以下是您要翻译的内容:
对于我的应用程序,我正在使用一个可绘制资源文件创建一个启动屏幕,该文件包含一个图层列表,其中包括一个带有有色背景和居中的矢量可绘制图标的图层。
splash_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/grey_700" />
<item
android:drawable="@drawable/splash_foreground"
android:gravity="center" />
</layer-list>
style.xml
...
<style name="Theme.SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:statusBarColor">@color/grey_900</item>
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
...
AndroidManifest.xml
...
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.SplashScreen"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
splash_foreground.xml
问题
输出应该显示splash_foreground
在屏幕中居中显示,而背景颜色应在整个屏幕上裁剪,但实际上显示的是一个居中裁剪的启动屏幕。
英文:
For my app I am creating an Splash Screen using a drawable resource file consist of a layerlist having a colored background and a centered vector drawable for the logo.
splash_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/grey_700" />
<item
android:drawable="@drawable/splash_foreground"
android:gravity="center" />
</layer-list>
style.xml
...
<style name="Theme.SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:statusBarColor">@color/grey_900</item>
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
...
AndroidManifest.xml
...
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.SplashScreen"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
splash_foreground.xml
Problem
the output should show splash_foreground
centered in the screen while the background color is cropped all over the screen but is showing a splash screen centered cropped.
答案1
得分: 1
I solved this problem by using the new splash_screen API. all thanks to this post
Solution
-
Add Splash Screen API in
build.gradle
implementation 'androidx.core:core-splashscreen:1.0.0'
-
Add this line before the application tag in the
AndroidManifest.xml
file
<uses-sdk tools:overrideLibrary="androidx.core.splashscreen" />
-
In
theme.xml
&theme.xml (night)
create a new style withTheme.SplashScreen
being parent
<item name="android:statusBarColor">@color/grey_900</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_app_foreground</item>
<item name="windowSplashScreenIconBackgroundColor">@color/grey_700</item>
<item name="windowSplashScreenBackground">@color/grey_700</item>
<item name="postSplashScreenTheme">@style/Theme.LGConnect</item>
</style> ```
- **Set application theme and Launcher Activity theme to the newly created style**
``` ...
<application
...
android:theme="@style/Theme.App.Starting"
...
<activity
...
android:theme="@style/Theme.App.Starting"
... ```
- **Inside `onCreate` method of `MainActivity` add this line**
``` ...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
... ```
[1]: https://stackoverflow.com/a/72320117/16177121
<details>
<summary>英文:</summary>
I solved this problem by using the new splash_screen API. all thanks to [this post][1]
## Solution ##
- **Add Splash Screen API in `build.gradle`**
implementation 'androidx.core:core-splashscreen:1.0.0'
- **Add this line before the application tag in the `AndroidManifest.xml` file**
<uses-sdk tools:overrideLibrary="androidx.core.splashscreen" />
- **In `theme.xml` & `theme.xml (night)` create a new style with `Theme.SplashScreen` being parent**
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="android:statusBarColor">@color/grey_900</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_app_foreground</item>
<item name="windowSplashScreenIconBackgroundColor">@color/grey_700</item>
<item name="windowSplashScreenBackground">@color/grey_700</item>
<item name="postSplashScreenTheme">@style/Theme.LGConnect</item>
</style>
- **Set application theme and Launcher Activity theme to the newly created style**
...
<application
...
android:theme="@style/Theme.App.Starting"
...
<activity
...
android:theme="@style/Theme.App.Starting"
...
- **Inside `onCreate` method of `MainActivity` add this line**
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
...
[1]: https://stackoverflow.com/a/72320117/16177121
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论