【发布时间】:2019-12-19 01:21:26
【问题描述】:
我尝试实现“斜线屏幕”,但遇到了一个问题,即初始屏幕上的图像没有真正居中,有时它会向上跳跃,有时会向下跳跃,有时它放置得很好。
首先,问题:
所以在这里你可以看到我的标志,第一个出现的是红色的,它是这样声明的:
background_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Background color -->
<item android:drawable="@color/colorPrimaryDark" />
<!-- Logo at the center of the screen -->
<item
android:width="300dp"
android:height="300dp"
android:gravity="center">
<bitmap android:src="@drawable/ic_launcher_sablier" />
</item>
</layer-list>
并添加如下:
styles.xml
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
然后显示带有蓝色的视图,它是这样声明的:
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayoutSplash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageViewSplash"
android:layout_width="300dp"
android:layout_height="300dp"
app:srcCompat="@drawable/ic_launcher_sablier" />
<ProgressBar
android:id="@+id/progressSplash"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="12dp"
android:indeterminateTint="@color/grey_light"
android:visibility="gone" />
</LinearLayout>
就像 windowBackground 考虑了其他东西(StatusBar 高度等),它并没有真正在屏幕上居中。
我试过了:
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
和/或
<item name="android:windowNoTitle">true</item>
和/或
<item name="android:windowContentOverlay">@drawable/background_splash</item>
没有任何效果。
有一刻我对自己说,如果它不能在 api 26 下运行也没关系,所以我使用了它:
v26/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowSplashscreenContent">@drawable/background_splash</item>
</style>
</resources>
同样的问题。
所以最后我想向上/向下移动膨胀的视图,而不是尝试正确放置窗口背景,如果它不完全位于屏幕中心,这不是一个大问题,所以我尝试删除状态栏高度,并且它在某些设备上运行,但在其他设备上运行,等等......
它正在使用我之前共享的特定 v26/style.xml 在 Nexus 5 上运行,所以这正是我想要的:
例如,在我的小米 MI 8 上会发生什么(它也发生在 Pixel 2XL、某些华为设备上……):
因此,如果有人有一个解决方案将 windowBackground 正确居中或相应地移动膨胀的视图,我会很满意这两个!
谢谢你帮助我!
编辑 13/08:
这里是您要求的 AppTheme:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
【问题讨论】:
-
你设置了重力center吗?将 Pb 的 GONE 更改为 INVISIBLE
-
@Skyle 尝试设置
LinearLayout属性android:fitsSystemWindows="true"。 -
@Piyush 我试过了,隐藏 progressBar 不是解决方案,因为现在它会向上跳而不是向下跳(由于 progressBar 的大小)
-
@mmmatey 我也试过了,但它似乎什么也没做:/
-
@Skyle 你也可以分享你的“appTheme”吗?所以我们可以检查问题是否来自那里。
标签: android layout background