【问题标题】:How to centre the same asset/drawables between Splash Screen Activity and other activity?如何在启动画面活动和其他活动之间居中相同的资产/可绘制对象?
【发布时间】:2018-10-03 01:33:18
【问题描述】:

我已经创建了没有任何布局 xml 文件的启动画面,但这是通过将应用程序徽标居中的矢量 drawable 完成的,如下所示:

 <item name="android:windowBackground">@drawable/splash_background</item>

但我也想在下一个后续屏幕中显示相同的应用程序徽标。对于等式在我的例子中,让我们说主活动屏幕,我有一个布局 xml,我用约束布局将图标居中。

activity_main.xml

    <!-- By adding android:layout_marginTop="26dp" to the ImageView kind of works but not sure why the magic number works
     Not sure where to get the same number for some other devices-->

    <ImageView
        android:id="@+id/imageViewLogo"
        android:layout_width="@dimen/splash_icon"
        android:layout_height="@dimen/splash_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_invest_logo" />


</android.support.constraint.ConstraintLayout>

但我注意到,当 Splash 和 Main Activity 之间发生相同的资产徽标交换时,我可以看到徽标的跳跃行为。

感谢您的帮助。下面是整个源代码的github。

https://github.com/nksaroj/InvestApp

你可以在这里看到绿色标志的跳跃问题

【问题讨论】:

    标签: android android-layout splash-screen


    【解决方案1】:
    getWindow().getDecorView().getRootView().getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    // Layout has happened here.
    
                    float statusBarHeight = getStatusBarHeight() * 1f;
    
                    //This is tricks where Samsung and some other devices don't consider the height in splash screen so that you need to adjust the height manually 
                    View navigationBarBackground = findViewById(android.R.id.navigationBarBackground);
    
                    if (navigationBarBackground == null) {
                        statusBarHeight = statusBarHeight * -1f;
                    }
    
                    imageViewIconY = imageViewReadyIcon.getY() + (statusBarHeight / 2);
    
                    imageViewIconY.setY(imageViewIconY);
    
    
    
                    // Don't forget to remove your listener when you are done with it.
                    getWindow().getDecorView().getRootView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多