如何在启动画面后制作相同的布局

5
我的启动画面是一个layer-list可绘制作为应用程序主题中的背景。以下是它的代码:

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/dark_blue"/>

    <item android:top="100dp">
        <bitmap
            android:gravity="top"
            android:src="@mipmap/img_logo"/>
    </item>

</layer-list>

正如您所看到的,我将Logo与顶部保持100dp的距离。然后我尝试在我的片段布局中做同样的事情: < p > < em > fragment_start.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/bg_create_account">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/img_logo"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp" />

</RelativeLayout>

但是在界面布局中的logo比启动屏幕上的logo位置低。我认为问题出现在Activity的默认边距上。但如果我设置:

<dimen name="activity_horizontal_margin">0dp</dimen>
<dimen name="activity_vertical_margin">0dp</dimen>

仍然没有任何反应。我总是看到标志从上往下“跳动”约10-20 dp。我该如何避免这种情况?

编辑:我的Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>

</LinearLayout>

EDIT 2: 我尝试手动调整距离,如果我设置<item android:top="125dp">(或126dp),并保留android:layout_marginTop="100dp",我就看不到“跳跃”了。这意味着差异为25或26 dp,但它们在哪里?
EDIT 3: 根据Bryan的答案,此问题仅存在于Android 4.4(API 19)及以上版本。为了避免这种情况,我在文件夹values-19中覆盖了styles.xml
<item name="android:windowTranslucentStatus">true</item>

你能发布屏幕截图吗? - Umar Ata
如果您的activity_main.xml文件中存在边距和填充,请将其删除。 - Umar Ata
根据我的想法,在启动屏幕中,您隐藏了 ToolBar,然后在片段中再次设置为 VISIBLE,这就是为什么它看起来像上下两个屏幕。 - Harshad Pansuriya
我添加了我的活动XML,没有填充或边距。ToolBar也不是原因。我在主题中和Activity/Fragment中都没有使用ToolBarActionBar - Alexander Tumanin
2个回答

2
似乎你用于启动画面的可绘制对象没有考虑到状态栏的大小,但Activity确实考虑了。这就是你观察到的大约25dp的差异,尽管这个高度的大约25dp不保证在所有设备上都是相同的。

没错!谢谢!它只在Android 4.4及以上版本中跳转。 - Alexander Tumanin

2
也许问题出在ActionBarSize上,试着在启动屏幕中添加以下代码: 没有AppCompat:
android:paddingTop="?android:attr/actionBarSize"

使用AppCompat

android:paddingTop="?attr/actionBarSize"

如果你愿意的话(虽然这可能被认为是不好的实践),你可以使用数据绑定在Activity布局中设置负padding:

android:paddingTop="@{-1 * ?android:actionBarSize}"

启动屏是一个layer-list。它没有android:paddingTop。不过问题已经解决了。 - Alexander Tumanin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接