【问题标题】:Are the layout bounds of a layer-list windowBackground different than an inflated layout?层列表 windowBackground 的布局边界与膨胀布局不同吗?
【发布时间】:2018-08-24 17:38:04
【问题描述】:

在我正在开发的应用程序上,我们有一个初始屏幕,其中包含一个 RelativeLayout 和一个位于中心的徽标(以及其他一些东西,例如加载微调器等):

fragment_splash_image.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:background="@drawable/loading_screen_bg"
    ... >
    <ImageView
        android:id="@+id/universal_loading_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/logo_large"
     ... />
    ... other stuff ...
</RelativeLayout>

为了确保在我们的启动屏幕之前不只是一个短暂的空白屏幕,我们在styles.xml 中有一个SplashTheme 用于活动。它的android:windowBackground 只是一个图层列表,logo 再次居中,希望 logo 出现在屏幕中间,而 fragment_splash_image 中的其他内容也出现。

splash_placeholder.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<item android:drawable="@drawable/loading_screen_gradient"/>
<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/logo_large"/>
</item>
</layer-list>

请注意,@drawable/logo_large 在每个中都是相同的徽标,并且位于每个屏幕的中心。预期的行为是它根本不应该移动。

无论如何,fragment_splash_image 在从 FrameLayout 扩展的类中膨胀,在这个方法中:

private void inflateContent() {
    final View splashImageFragment = LayoutInflater.from(getContext()).inflate(R.layout.fragment_splash_image, this, true);
    final ImageView brandLogo = (ImageView) splashImageFragment.findViewById(R.id.universal_loading_logo);
    final int statusBarHeight = ScreenUtils.getStatusBarHeight(getResources());
    final int navBarHeight = !ScreenUtils.hasSoftNavBar() ? 0 : ScreenUtils.getNavigationBarHeight(getResources());
    brandLogo.setPadding(0, 0, 0, statusBarHeight - navBarHeight);
}

现在,这里发生的事情是,我们最初只是按原样膨胀片段。不幸的是,这会导致飞溅片段中的徽标与飞溅占位符的徽标相比向上或向下跳跃一小段距离,具体取决于测试的设备。在我的 Galaxy S6 手机上,我认为占位符闪屏可能包含状态栏高度,因此我将其添加为徽标底部的填充。该设备的问题已解决。然而,在带有软导航栏的 Nexus 7 上,logo 仍然跳得很远。我得出的结论是,它可能还包括布局边界中的导航栏高度,并写下了您在上面看到的内容:bottom padding = statusBarHeight - navBarHeight,对于带有硬导航按钮的设备,navBarHeight 为 0。

这适用于两种设备...然后我在 Google Pixel 上进行了测试。标志跳了下来。如果我将底部填充设置为 0,并且 top 填充设置为状态栏高度,这仅适用于像素。

我被难住了。到底是什么决定了这两种布局的高度?它们明显不同,我不确定如何确保徽标不会在任何设备上从一个屏幕跳到另一个屏幕。提前致谢!

【问题讨论】:

    标签: android


    【解决方案1】:

    在 API 21+ 上添加 &lt;item name="android:windowDrawsSystemBarBackgrounds"&gt;false&lt;/item&gt; 可以让您为所有设备保留相同的公式。

    我遇到了类似的问题,正在考虑走这条路。

    【讨论】:

      【解决方案2】:

      Sajal 的答案是前进的方向。

      在您的 styles.xml 文件中,为您的启动画面添加一个主题,例如:

      <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="android:windowBackground">@drawable/background_splash</item>
          <item name="android:windowDrawsSystemBarBackgrounds">false</item>
      </style>
      

      @drawable/background_splash 是您的初始屏幕&lt;layer-list&gt; 定义。这将确保启动画面不会位于状态栏/导航栏下方。

      【讨论】:

        猜你喜欢
        • 2019-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多