【问题标题】:Centered image on windowBackground is different than on an inflated layoutwindowBackground 上的居中图像与膨胀布局上的图像不同
【发布时间】: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


【解决方案1】:

所以,经过大量研究,我设法使其适用于 API 26 及更高版本。

让我们回顾一下,如果您只是想确保在启动应用程序时显示图像以避免“白屏加载”,只需使用:

styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.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>

    <style name="SplashTheme" parent="AppTheme">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>
</resources>

然后这样设置:

AndroidManifest.xml

<activity
    android:name=".ui.splash.ActivitySplash"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

现在,如果您想通过显示动画进度条来获得流畅的屏幕流,您可以这样做,但仅适用于 API 26 及更高版本,如下所示:

v26/style.xml

<resources>
    <style name="SplashTheme" parent="AppTheme">
        <item name="android:windowSplashscreenContent">@drawable/background_splash</item>
        <item name="android:windowOverscan">true</item>
    </style>
</resources>

解决方案是使用android:windowOverscan,我真的不知道为什么,我试图在doc中搜索答案,但我发现的只是:

windowOverscan

指示此窗口是否应扩展到过扫描区域的标志。 对应WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN。

可以是布尔值,例如“true”或“false”。

为了让它在 API 26 中工作,也许有一些计算需要借助这个来完成(我承认我没有时间或耐心花更多时间来解决这个问题):

https://developer.android.com/reference/androidx/core/view/WindowInsetsCompat

享受吧。

(如果有人对这个问题有更好的答案,我可以检查他的答案而不是我的答案;))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    相关资源
    最近更新 更多