使ImageView相对于屏幕居中(忽略状态栏和导航栏)

Lia*_*uch 5 layout android

我正在使用react native初始屏幕包(https://github.com/crazycodeboy/react-native-splash-screen),并试图在显示徽标的Android中实现适当的初始屏幕。

我最初在上面找到了这篇文章,覆盖得很好:https : //medium.com/handlebar-labs/how-to-add-a-splash-screen-to-a-react-native-app-ios-和android-30a3cec835ae-但是,我有文章解释的“跳跃问题”,但作者可能没想到。

让我们解释一下:首次打开应用程序时,启动活动上会显示一个基于图层列表的启动屏幕,launch_screen.xml当应用程序加载到内存中时,该屏幕会转换为react-native-splash-screen活动(由定义)。问题在于,尽管启动活动中的主题充满了整个屏幕,但在launch_screen.xml中定义的初始屏幕的第二阶段确实关心布局。因此,“中心徽标”的上下偏移量为24dp,具体取决于android设备是否具有软导航按钮。

如果它是一个恒定的偏移量,那么我将仅使用margin来偏移徽标,但是由于导航按钮的原因,我需要在xml中使用某种条件,该条件可以检测并响应现有的导航栏。

因此,我想将a对准ImageView屏幕的中心,而不是其父容器。如果存在的话,或者在导航栏下方以某种方式存在父容器。

或者换句话说,问题在于飞溅的第一阶段相对于屏幕居中,而第二阶段相对于屏幕可用空间居中,我想在某些情况下将两者匹配无论导航栏是否存在均有效的方式

这可能吗?

launch_screen.xml (第二阶段的布局)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:background="@color/background">

    <TextView android:text="App"
         android:layout_alignParentBottom="true"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:paddingBottom="20dp"
         android:gravity="center"/>



    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@mipmap/logo"/>

    </LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

background_splash.xml (在初始阶段的主题使用)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/background"/>

    <item
        android:width="200dp"
        android:height="200dp"
        android:drawable="@mipmap/logo"
        android:gravity="center"
         />

</layer-list>
Run Code Online (Sandbox Code Playgroud)

Zat*_*tes 2

您可以通过编程方式偏移启动屏幕,如下所示

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ImageView image = view.findViewById(R.id.image_logo);
    image.setTranslationY((getNavBarHeight() - getStatusBarHeight()) / 2.0f);

}

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

public int getNavBarHeight(){
    Resources resources = getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)